2016-07-18 42 views
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{ 

      } 


     } 

    } 

在此先感谢!

+0

请添加保存实体代码。 –

+0

感谢您的回复,我现在也分享了保存代码。 –

+0

“插入为空”是什么意思?交易完成后它是否为空?或者,当它被插入到数据库中时它是空的(因为Hibernate会这样做,在外键中插入具有空值的行,然后在行上执行'update')。 – Tobb

回答

0

您不提供完整的代码。所以我可以做一个假设。您不要将SurveyConductBurglaryDetails设置为SurveyConductBurglaryProperty。您的代码应如下所示:

SurveyConductBurglaryDetails details = new SurveyConductBurglaryDetails(); 
details.setSurveyConductBurglaryProperties(
    new ArrayList<SurveyConductBurglaryProperty>()); 

SurveyConductBurglaryProperty property = new SurveyConductBurglaryProperty(); 
property.setSurveyConductBurglaryDetails(details); 

details.getSurveyConductBurglaryProperties().add(property); 

session.save(details);