org.hibernate.exception.GenericJDBCException: 无法在休眠状态下执行语句

org.hibernate.exception.GenericJDBCException: could not execute statement in hibernate

我正在尝试使用可嵌入注释在一个对象中存储不同的值,以便持久保存在一个对象中 table。

我有两个 class: 用户详细信息:

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. 可嵌入的地址 class 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;
    }
    

    } 这是我的主要 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();
    

    }

我试图存储数据,但它显示:

   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

我的地址中有密码,但它显示错误,所以我删除了它,但它仍然显示该错误。

请帮我解决一下

您数据库中的 table 有一个字段 pincode,您尚未为其定义默认值且不可为 null。但是,您的实体没有该字段的映射,并且您没有为该字段发送值。因此,java.sql.SQLException:字段 'pincode' 没有默认值

1) 调整您的实体,使其包含字段 "pincode"

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

2) 或更改基础 table 以便 pincode 字段具有默认值:

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

Number(10) 只是作为示例。将其替换为密码的可变类型。

我遇到了与 org.hibernate.exception.GenericJDBCException 相同的错误:无法执行语句 我忘记初始化会话工厂。

初始化(如下)会话工厂解决了我的问题。

@Autowired
private SessionFactory sessionFactory;

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