【问题标题】:Hibernate foreign key always inserted as NULL休眠外键始终插入为 NULL
【发布时间】:2016-07-18 19:32:51
【问题描述】:

我在下面的休眠实体中遇到问题。 在将记录插入基表时,外键始终作为 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{

            }


        }

    }

提前致谢!!!

【问题讨论】:

  • 请添加保存实体代码。
  • 感谢回复,我也分享了存档码。
  • “插入为空”是什么意思?交易完成后是否为空?或者当它是insert-ed 到数据库时它是空的(因为 Hibernate 这样做,在外键中插入具有空值的行,然后在行上执行 update..)
  • 顺便说一句,你的表名看起来像是魔鬼自己写的。你应该瞄准人类可以理解的东西..

标签: hibernate hibernate-mapping


【解决方案1】:

您没有提供完整的代码。所以我可以做一个假设。您没有将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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多