简体   繁体   English

org.hibernate.exception.GenericJDBCException:无法在hibernate中执行语句

[英]org.hibernate.exception.GenericJDBCException: could not execute statement in hibernate

I am trying to use embeddable annotation to store different values in one object to be persisted in one table. 我试图使用嵌入式注释将一个对象中的不同值存储在一个表中。

I have two classes: userDetails: 我有两个类:userDetails:

package org.hibernet.src;
@Entity
@Table (name="UserDetails")
 public class userDetails {
 @Id 
@GeneratedValue(strategy = GenerationType.AUTO)
private int userId;
@Temporal(TemporalType.DATE)
private Date dob;
public Date getDob() {
    return dob;
}
public void setDob(Date dob) {
    this.dob = dob;
}
private String userName;
@Embedded
private Address address;

public Address getAddress() {
    return address;
}
public void setAddress(Address address) {
    this.address = address;
}
public int getUserId() {
    return userId;
}
public void setUserId(int userId) {
    this.userId = userId;
}
public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}

}
  1. Address class which is embeddable class: 地址类是可嵌入的类:

      @Embeddable public class Address { @Column(nullable=true) private String street; @Column(nullable=true) private String city; @Column(nullable=true) private String state; public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } 

    } this is my main class: 这是我的主要课程:

     public static void main(String[] args) { userDetails user = new userDetails(); Address add = new Address(); user.setUserName("Dipesh"); user.setDob(new Time(0)); add.setCity("vadodara"); add.setState("gujarat"); add.setStreet("raopura"); SessionFactory sessionfactory = new Configuration().configure().buildSessionFactory(); Session session = sessionfactory.openSession(); session.beginTransaction(); session.save(user); session.save(add); /*user = (userDetails)session.get(userDetails.class, 6);*/ session.getTransaction().commit(); session.close(); 

    } }

I was trying to store the data but it was showing: 我试图存储数据,但显示如下:

   ERROR: Field 'pincode' 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:54)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:490)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:195)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:179)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:214)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)
at org.hibernate.test.userTest.main(userTest.java:36)
Caused by: java.sql.SQLException: Field 'pincode' doesn't have a default    
value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1056)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1837)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1961)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2543)
at     
    com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1737)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2022)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1940)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1925)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
... 22 more

I had pincode in my address but it was showing an error so i removed it but still it showing that error. 我在地址中输入了密码,但它显示了错误,因此我删除了它,但仍然显示该错误。

Please help me to solve it. 请帮我解决一下。

The table in your db has a field pincode for which you've not defined a default value and is not nullable. 数据库中的表有一个字段密码,您没有为其定义默认值且不可为空。 However, your entity has no mapping for that field and you are not sending a value for this field. 但是,您的实体没有该字段的映射,并且您没有为此字段发送值。 Thus, java.sql.SQLException: Field 'pincode' doesn't have a default 因此, java.sql.SQLException:字段'pincode'没有默认值
value

1) Adapt your entity that it includes the field "pincode" 1)调整您的实体,包括字段“pincode”

@Entity
@Table (name="UserDetails")
public class userDetails {
...
@Column(name="pincode", columnDefinition="Number(10) default '34562'")
int pincode;
...

2) or alter the underlying table so that the pincode field has a default value: 2)或更改基础表,以便pincode字段具有默认值:

 ALTER TABLE user_details MODIFY pincode NUMBER(10) NULL DEFAULT NULL;

Number(10) is just chosen as an example. 仅选择数字(10)作为示例。 Replace this by the variable type of pincode. 用可变类型的pincode替换它。

I had same error as org.hibernate.exception.GenericJDBCException: could not execute statement where i forgot to initialize session factory. 我有与org.hibernate.exception.GenericJDBCException相同的错误:无法执行语句我忘记初始化会话工厂。

Initializing(as follow) session factory resolved my issue. 初始化(如下)会话工厂解决了我的问题。

@Autowired
private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.hibernate.exception.GenericJDBCException:无法执行语句 - org.hibernate.exception.GenericJDBCException: could not execute statement 错误信息:无法执行语句; 嵌套异常是 org.hibernate.exception.GenericJDBCException:无法执行语句 - Error message: could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement “消息”:“无法执行语句; 嵌套异常是 org.hibernate.exception.GenericJDBCException: could not execute statement”, - “message”: “could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement”, ested异常是org.hibernate.exception.GenericJDBCException:无法执行查询 - ested exception is org.hibernate.exception.GenericJDBCException: could not execute query org.hibernate.exception.GenericJDBCException:无法执行查询]的根本原因 - org.hibernate.exception.GenericJDBCException: could not execute query] with root cause org.hibernate.exception.GenericJDBCException:无法执行查询 - org.hibernate.exception.GenericJDBCException: could not execute query org.hibernate.exception.GenericJDBCException:无法执行查询 - org.hibernate.exception.GenericJDBCException: could not execute query substr错误:org.hibernate.exception.GenericJDBCException:无法执行查询 - substr error: org.hibernate.exception.GenericJDBCException: could not execute query org.hibernate.exception.GenericJDBCException:无法插入: - org.hibernate.exception.GenericJDBCException: could not insert: Ha sido不可能ejecutar la acción Grabar:org.hibernate.exception.GenericJDBCException:无法执行语句 - Ha sido imposible ejecutar la acción Grabar: org.hibernate.exception.GenericJDBCException: could not execute statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM