休眠错误:在调用 save() 之前,必须手动分配此类的 id:

2022-08-31 10:42:44
Caused by: org.springframework.orm.hibernate3.HibernateSystemException: ids for this class must be manually assigned before calling save(): com.rfid.model.Role; nested exception is org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.rfid.model.Role
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:676)
    at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
    at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:748)
    at com.wfos.engine.wrapper.domain.impl.WrapperImpl.save(WrapperImpl.java:159)
    ... 47 more
    Caused by: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.rfid.model.Role
    at org.hibernate.id.Assigned.generate(Assigned.java:53)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
    at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:685)
    at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:677)
    at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:673)
    at org.springframework.orm.hibernate3.HibernateTemplate$16.doInHibernate(HibernateTemplate.java:751)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
    ... 50 more
    WARN [21:14:21] (CommonsLoggingOutput.java:59): - --Erroring: batchId[1] message[java.lang.reflect.UndeclaredThrowableException]

我的班级是这样的:

@Entity
@javax.persistence.Table(name="Role")
@Table(appliesTo = "Role")
public class Role {

@Id  
@Column(name="U_id")
public String U_id;

public String U_pwd;

public String U_account;

public String U_mode;

public String U_status;


public String getU_pwd() {
    return U_pwd;
}

public void setU_pwd(String u_pwd) {
    U_pwd = u_pwd;
}

public String getU_account() {
    return U_account;
}

public void setU_account(String u_account) {
    U_account = u_account;
}

public String getU_id() {
    return U_id;
}

public void setU_id(String u_id) {
    U_id = u_id;
}

public String getU_mode() {
    return U_mode;
}

public void setU_mode(String u_mode) {
    U_mode = u_mode;
}

public String getU_status() {
    return U_status;
}

public void setU_status(String u_status) {
    U_status = u_status;
}

}

答案 1

您的类的字段具有类型,因此无法为您生成 id。@EntityString@Id

如果在 DB 中将其更改为自动增量,在 java 中更改为 a,然后添加注释:Long@GeneratedValue

@Id
@Column(name="U_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long U_id;

它将为您处理递增的id生成。


答案 2

已使用 Oracle 数据库中定义的序列 ID 解决了此问题。

ORACLE_DB_SEQ_ID定义为表的序列。另请查看控制台以查看用于验证的休眠 SQL。

@Id
@Column(name = "MY_ID", unique = true, nullable = false)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "id_Sequence")
@SequenceGenerator(name = "id_Sequence", sequenceName = "ORACLE_DB_SEQ_ID")
Long myId;

推荐