本文介绍了是否有可能没有对象到对象的映射执行外键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设提供以下映射:

 < class name =Atable =a_table> 
< id name =id/>
< / class>

< class name =Btable =b_table>
< id name =id/>
< / class>

Java class:

  public class A {
private long id;
私人B entityB;
// getters and setters skipped
}

是否可以更改Hibernate映射使得外键在启动时仍由Hibernate执行和创建,但类 A 看起来像下面这样:

  public class A {
private long id;
私人长IDOFB;
// getters and setters skipped
}

据我所知,如果我转换<多对一... 转换成< property ... key不会被数据库强制执行。

我需要这样做,因为对象 B 可能(或者可能不会) )被单独初始化,有时会导致
org.hibernate.LazyInitializationException:无法初始化代理 - 没有Session a.getB ()被调用。我宁愿将它作为 long idOfB ,并在需要时加载整个对象。这也会使加载对象 A 更快。



我相信我的问题非常类似于,但提供的解决方案(使用延迟加载)在我的情况下是不适合的,即使我调用 a.getB()。getId(),我会得到 LazyInitializationException ,而如果我打电话给 a.getIdOfB()我不会。




解决方案
如上所述

所以我的建议是:同时使用

  public class EntityA {

private Integer idOfB;

私人EntityB entityB;

// getter's and setter's

}



 < class name =Atable =a_table> 
< id name =id/>
< / class>

请注意,当两个属性共享同一列时,必须将其置于一个属性。否则,Hibernate会抱怨一些错误。它解释了为什么我在entityB属性中定义update =false和insert =false。



问候,

Assuming the following mappings are provided:

<class name="A" table="a_table">
  <id name="id"/>
  <many-to-one name="entityB" column="fk_B" not-null="false" unique="true"/>
</class>

<class name="B" table="b_table">
  <id name="id"/>
</class>

Java class:

public class A {
   private long id;
   private B entityB;
   // getters and setters skipped
}

Is it possible to change the Hibernate mapping so that foreign key is still enforced and created by Hibernate upon startup, but class A would look like as the following:

public class A {
   private long id;
   private long idOfB;
   // getters and setters skipped
}

I understand that if I convert <many-to-one... into a <property... this would work, but foreign key would not be enforced by the database.

I need to do this because object B might (or might not) be initialized separately which sometimes causesorg.hibernate.LazyInitializationException: could not initialize proxy - no Session exceptions to occur when a.getB() is called. I would prefer to have it as a long idOfB and load whole object whenever is necessary; this would also make loading of object A quicker.

I believe my question is very similar to this one, yet the offered solution (to use lazy loading) is not appropriate in my case as even if I call a.getB().getId(), I'd get LazyInitializationException whereas if I call a.getIdOfB() I wouldn't.

Thanks very much in advance.

解决方案

As said

So my advice is: use both

public class EntityA {

    private Integer idOfB;

    private EntityB entityB;

    // getter's and setter's

}

And

<class name="A" table="a_table">
    <id name="id"/>
    <property name="idOfB" column="fk_B" not-null="false" unique="true"/>
    <many-to-one name="entityB" update="false" insert="false" column="fk_B"/>
</class>

Notice when two properties share the same column, you have to put settings about it in just one property. Otherwise, Hibernate will complain some errors. It explains why i define update="false" and insert="false" in entityB property.

regards,

这篇关于是否有可能没有对象到对象的映射执行外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 08:59
查看更多