0
我在下面的hibernate實體中遇到問題。 將記錄插入基表時,外鍵總是作爲NULLin子表插入。 請幫忙,我有沒有在這裏配置任何符號?Hibernate外鍵始終插入爲NULL
//PK
@Entity
@Table(name="s_c_bur_dtl")
public class SurveyConductBurglaryDetails {
@Id
@GeneratedValue(strategy = GenerationType.AUTO,generator="surveycondbgdtl_seq")
@javax.persistence.SequenceGenerator(name="surveycondbgdtl_seq",sequenceName="surveycondbgdtl_seq",allocationSize=1,initialValue=1)
@Column(name = "nsurveycondbgdtlcd", unique=true, nullable = false, columnDefinition = "bigint")
private BigInteger surveyCondBgDtlCd;
@OneToMany(cascade = CascadeType.ALL,mappedBy="surveyConductBurglaryDetails")
private List<SurveyConductBurglaryProperty> surveyConductBurglaryProperties;
}
//FK
@Entity
@Table(name="s_c_bur_property")
public class SurveyConductBurglaryProperty {
@Id
@GeneratedValue(strategy = GenerationType.AUTO,generator="surveycondbgproperty_seq")
@javax.persistence.SequenceGenerator(name="surveycondbgproperty_seq",sequenceName="surveycondbgproperty_seq",allocationSize=1,initialValue=1)
@Column(name = "nsurveycondbgpropertycd", unique=true, nullable = false, columnDefinition = "bigint")
private BigInteger surveyCondBgPropertyCd;
@ManyToOne
@JoinColumn(name = "nsurveycondbgdtlcd")
private SurveyConductBurglaryDetails surveyConductBurglaryDetails;
}
@Service
class SaveService{
@Autowired DbService dbService;
public void conductSurvey(SurveyConductBurglaryDetails surveyConductBurglaryDetails) throws IllegalAccessException, InvocationTargetException{
dbService.saveOrUpdate(surveyConductBurglaryDetails); // call for save
}
}
@Service
class DbService{
public void saveOrUpdate(Object insertObj){
Session session = surveyTransactionManager.getSessionFactory().getCurrentSession();
Transaction transaction = session.getTransaction();
if(!transaction.isActive()){
session.beginTransaction();
}
try{
session.saveOrUpdate(insertObj);
transaction.commit();
}catch(Exception e){
transaction.rollback();
// TODO Auto-generated catch block
if(logger.isDebugEnabled()){
logger.debug("SurveyDBService.saveOrUpdate Catch block");
}
e.printStackTrace();
}finally{
}
}
}
在此先感謝!
請添加保存實體代碼。 –
感謝您的回覆,我現在也分享了保存代碼。 –
「插入爲空」是什麼意思?交易完成後它是否爲空?或者,當它被插入到數據庫中時它是空的(因爲Hibernate會這樣做,在外鍵中插入具有空值的行,然後在行上執行'update')。 – Tobb