gpt4 book ai didi

java - 字段 'Id_employeer' 没有默认值

转载 作者:行者123 更新时间:2023-11-29 10:34:50 25 4
gpt4 key购买 nike

我不明白为什么我的应用会抛出异常“Field 'Id_employeer' does not have a default value”。

Id_employeer 是我的主键,当我尝试将任何信息保存到数据库时,我遇到了该异常,但是,当我尝试从数据库读取信息时,它正在工作。

我阅读了 StackOverflow 上的主题,但我仍然不明白我的应用程序中的答案。我正在使用 Hibernate 5.2.11。

我的CharacterInfo.hbm.xml:

    <?xml version="1.0"?>

<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="CharacterInfo" table="employees">
<id name="Id" column="id_employeer">
<generator class="native"/>
</id>
<property name="Login" column="employeer_login"/>



</class>

</hibernate-mapping>

我的 HibernateUtility:

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import java.util.List;


public class HibernateUtility {


SessionFactory sessionFactory;

public void saving(Object object){
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save( object);
//session.save( new CharacterInfo().getLogin());
session.getTransaction().commit();
session.close();
}

public void reading(){
Session session = sessionFactory.openSession();
session.beginTransaction();
List result = session.createQuery("from CharacterInfo").list();
for ( CharacterInfo characterInfo : (List<CharacterInfo>) result ) {
System.out.println( "CharacterInfo (" + characterInfo.getLogin() + ") : " + characterInfo.getLogin() );
}
session.getTransaction().commit();
session.close();
}


protected void setUp() throws Exception {

final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure()
.build();

try {
sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();


} catch (Exception e) {
StandardServiceRegistryBuilder.destroy(registry);
}
}




}

下一个CharacterInfo类

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;


@Entity
@Table( name = "employees")
public class CharacterInfo {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "Id_employeer")
private int id ;

@Column(name = "employeer_Login")
private String login;


public CharacterInfo() {}

public int getId() {
return id;

}

public void setId(int id) {
this.id = id;
}


public String getLogin() {
return login;
}

public void setLogin(String login) {
this.login = login;
}



}

主类

public class main {


public static void main(String args[]) throws Exception {
HibernateUtility hibernateUtility = new HibernateUtility();
CharacterInfo characterInfo = new CharacterInfo();
characterInfo.setId(1);
characterInfo.setLogin("PawJaw");
hibernateUtility.setUp();
hibernateUtility.saving(characterInfo);


}
}

最后出现异常

ERROR: Field 'Id_employeer' doesn't have a default value Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute statement at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:178) at org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:57) at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2919) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3490) at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81) at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:626) at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:280) at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:261) at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:306) at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:318) at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:275) at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:182) at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:113) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192) at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177) at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73) at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:691) at org.hibernate.internal.SessionImpl.save(SessionImpl.java:683) at org.hibernate.internal.SessionImpl.save(SessionImpl.java:678) at HibernateUtility.saving(HibernateUtility.java:17) at main.main(main.java:10) Caused by: java.sql.SQLException: Field 'Id_employeer' doesn't have a default value at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2487) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858) at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2079) at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013) at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:175) ... 23 more

最佳答案

首先,如果您使用 Hibernate 注释,则不需要使用 Hibernate XML 映射文件。

您的错误可能是由于您的数据库架构造成的。 Id_employeer 列不是自动递增的。

关于java - 字段 'Id_employeer' 没有默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46631844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com