提交实体时出现以下错误。根据错误,我必须为实体分配一个 id,但我希望它必须由 JPA 本身处理。
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): alarm.ServiceAlarmConfEntity
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1214)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1147)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1153)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:678)
at alarm.Test.main(Test.java:32)
Caused by: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): alarm.ServiceAlarmConfEntity
at org.hibernate.id.Assigned.generate(Assigned.java:53)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:69)
我有两个具有一对一关系的实体。
@Entity(name ="services")
@Table(name = "services")
public class Service implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Column(name = "label")
private String label;
@Column(name = "schemapath")
private String schemapath;
@Column(name = "customizedPath")
private String customizedPath;
@Column(name = "birimId")
private Integer birimId;
@Column(name = "alarmthresholdXML")
private String alarmthresholdXML;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "service", fetch = FetchType.LAZY)
private ServiceAlarmConfEntity serviceAlarmConfEntity;
@Entity(name ="service_alarm_conf")
@Table(name = "service_alarm_conf")
public class ServiceAlarmConfEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "service_id")
private Integer serviceId;
@Lob
@Column(name = "alarmthresholdXML")
private String alarmthresholdXML;
@Column(name = "alarmCheckEnable")
private Integer alarmCheckEnable;
@Column(name = "alarmDataTransferTimeInSeconds")
private Integer alarmDataTransferTimeInSeconds;
@JoinColumn(name = "service_id", referencedColumnName = "id", insertable = false, updatable = false)
@OneToOne(optional = false, fetch = FetchType.LAZY)
private Service service;
主类:
package alarm;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class Test {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("alarm");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
Service service = new Service();
service.setLabel("Test1");
service.setBirimId(16);
service.setSchemapath("test1");
service.setCustomizedPath("Test1");
ServiceAlarmConfEntity serviceAlarmConfEntity = new ServiceAlarmConfEntity();
serviceAlarmConfEntity.setAlarmCheckEnable(1);
serviceAlarmConfEntity.setAlarmthresholdXML("alarmThresholdXML");
serviceAlarmConfEntity.setService(service);
service.setServiceAlarmConfEntity(serviceAlarmConfEntity);
tx.begin();
em.persist(service);
tx.commit();
em.close();
}
}
更新:
ServiceAlarmConfEntity 配置如下。它有效。
@MapsId
@JoinColumn(name = "service_id", referencedColumnName = "id")
@OneToOne(optional = false, fetch = FetchType.LAZY)
private Service service;