java - Hibernate String 类型外键问题

标签 java mysql hibernate hibernate-mapping

我试图通过将第一个表列(字符串类型)声明为第二个表的外键,以一对一映射方式连接两个表。

这是我主要的两个 hibernate 实体:

主要实体研究详细信息:

@Entity
@Table(name="StudyDetails")
public class StudyDetails {

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name="Id")
private Integer id;

@Column(name="InternalID", unique = true, nullable = false)
private String internalId;

@Column(name="StudyInstanceUID", unique = true)
private String studyInstanceUID;

@Column(name="LastUploadedDate")
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime lastUploadedDate; 

@Column(name="IsProcessed")
private boolean isProcessed;

@Column(name="IsUploaded")
private boolean isUploaded;

和第二个 DicomSeriesMeta:

@Entity
@Table(name="DicomSeriesMeta")
public class DicomSeriesMeta {

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name="Id")
private Integer id;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="InternalID", referencedColumnName="InternalID")
private StudyDetails studyDetails;

public StudyDetails getStudyDetails() {
    return studyDetails;
}

public void setStudyDetails (StudyDetails studyDetails) {
    this.studyDetails = studyDetails;
}

@Column(name="PatientID")
private String patientID;

@Column(name="IssuerOfPatientID")
private String issuerOfPatientID;

@Column(name="PatientSex")
private String patientSex;

@Column(name="PatientName")
private String patientName;

@Column(name="PatientBirthDate")
private String patientBirthDate;
...
}

我希望将 StudyDetails.InternalID 作为表 DicomSeriesMeta 的外键...它是使用特定算法生成的 String 类型的唯一 ID。

我正在尝试在数据库中保存 DicomSeriesMeta 对象,例如:

....
DicomSeriesMeta dicomSeriesMeta = new DicomSeriesMeta();
dicomSeriesMeta.setPatientID(patientID);
dicomSeriesMeta.setIssuerOfPatientID(issuerOfPatientID);
dicomSeriesMeta.setPatientSex(patientSex);
dicomSeriesMeta.setPatientName(patientName);
dicomSeriesMeta.setPatientBirthDate(patientBirthDate);
dicomSeriesMeta.setOtherPatientIDs(otherPatientIDs);
....
dicomSeriesMeta.setStudyDetails(getStudyDetailsbyStudyId(studyInstanceUID));
Session s=HibernateUtil.openSession();
s.beginTransaction();
s.save(dicomSeriesMeta);
s.getTransaction().commit();
s.close();
....

但是我得到:

 Hibernate: insert into DicomSeriesMeta (AccessionNumber, InstitutionAddress, InstitutionName,    IssuerOfPatientID, MediaStorageSOPInstanceUID, OperatorsName, OtherPatientIDs, OtherPatientIDsSequence, PatientAge, PatientBirthDate, PatientID, PatientName, PatientSex, PhysiciansOfRecord, ReferringPhysicianName, RequestingPhysician, SOPInstanceUID, SeriesInstanceUID, StudyDate, InternalID, StudyID, StudyInstanceUID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  Νοε 24, 2014 4:03:16 ΜΜ com.npap.dicomrouter2.FXMLDocumentController startDcmrcvService
  SEVERE: null
  org.hibernate.exception.GenericJDBCException: could not execute statement
at    org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54) at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:136)
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:2975)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3487)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:214)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:194)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:178)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:321)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:286)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:192)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:191)
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:764)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:756)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:752)
at com.npap.utils.ProcessDicomFiles.storeAnonymizationData(ProcessDicomFiles.java:323)
at com.npap.utils.ProcessDicomFiles.anonymizeDicomObj(ProcessDicomFiles.java:233)
at com.npap.utils.AnomynizerFileVisitor.visitFile(AnomynizerFileVisitor.java:24)
at com.npap.utils.AnomynizerFileVisitor.visitFile(AnomynizerFileVisitor.java:15)
at java.nio.file.Files.walkFileTree(Files.java:2670)
at java.nio.file.Files.walkFileTree(Files.java:2742)
at com.npap.utils.ProcessDicomFiles.anonymizeStudyFolders(ProcessDicomFiles.java:55)
at com.npap.dicomrouter2.FXMLDocumentController.startDcmrcvService(FXMLDocumentController.java:219)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1757)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1645)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8216)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3724)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3452)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1728)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2461)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:348)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:273)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:382)
at com.sun.glass.ui.View.handleMouseEvent(View.java:553)
at com.sun.glass.ui.View.notifyMouse(View.java:925)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/128893786.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
  Caused by: java.sql.SQLException: Field 'Id' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2794)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:133)
... 90 more

最佳答案

我认为您可以通过 Hibernate 实体自动生成表。在这种情况下,您需要指定生成策略。选择您需要的: https://docs.oracle.com/javaee/5/api/javax/persistence/GenerationType.html

关于java - Hibernate String 类型外键问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27106361/

相关文章:

mysql - 使用mysql的特定月份年份的周范围

java - 使用连接表在 Hibernate 中分页

java - 了解问题,从编程层模型的角度来看,在 Hibernate 中使用 .save() 和 .commit() 的正确方法是什么

java - Eclipse 3.5.1 编译器错误 :The type OutputFormat is not accessible due to restriction on required library . ./rt.jar

php - 通过电子邮件的用户通知系统

mysql - 如何使用 SQL 将小时范围转换为总小时数?

java - Hibernate:如何连接两个表,其中一个表没有ID?

java - HashMap 的奇怪序列化行为

java - 从关系中级联实体时出现 hibernate 问题

java - 如何模拟客户端和服务器之间的套接字断开连接(在 Windows 上)?