简体   繁体   English

了解/设置休眠配置

[英]Understanding/Setting up hibernate configuration

I'm trying to understand an existing application using hibernate which I have a few knowledge about. 我正在尝试使用休眠技术来了解现有的应用程序,而我对此有一些了解。 In this application, a database schema has been created, and classes have been automatically generated from the DB schema. 在此应用程序中,已创建数据库模式,并且已从数据库模式自动生成类。

Let's take an example of 2 tables in the DB: 让我们以数据库中的两个表为例:

   PERSON(id, firstname, surname)
   OBJECT(id, owner_id, description)

In the table OBJECT , owner_id would be a foreign key to PERSON.id . 在该表OBJECTowner_id将是一个外键PERSON.id

The generated classes would look approximately like that: 生成的类大致如下所示:

class Person {
  // ok for the following
  int id; // with getters and setters
  String firstname; // with getters and setters
  String surname; // with getters and setters

  // dunno why the following ?
  Set objects = new HashSet(0);
  Set getObjects() { return this.objects; }
  void setObjects(Set objects)  this.objects = objects; }
}

class Object {

   int id; // with getters and setters 
   Person owner; // with getters and setters
   String description;  // with getters and setters
}

First, I dont know why is there a reference to the set of objects owned in the generated class Person . 首先,我不知道为什么要对生成的类Person拥有的对象集进行引用。 Second, if I get an Object or a List<Object> through an HQL query, is the Person member of this Object fully loaded? 其次,如果我通过HQL查询获得ObjectList<Object> ,则此ObjectPerson成员是否已完全加载? How should I set up hibernate to ensure that it is? 我应该如何设置休眠以确保其休眠?

I'm asking because in my application, I have the hibernate session running as a separate executable, and another, deported application which communicate with the hibernate app and asks it to send some persistent objects. 我之所以问是因为,在我的应用程序中,我将休眠会话作为单独的可执行文件运行,而另一个驱逐出境的应用程序与休眠应用程序进行通信,并要求它发送一些持久对象。 I can get an Object but if on this object I make object.getPerson().getFirstname() , I run into an org.hibernate.LazyInitializationException: could not initialize proxy - no Session 我可以获取一个Object但是如果在此对象上创建object.getPerson().getFirstname() ,则会遇到org.hibernate.LazyInitializationException: could not initialize proxy - no Session

You're getting the LazyInitializationException because, in your client application, the values for your 'Person' field in your 'Object' object are not initialized (they are without assigned values). 之所以得到LazyInitializationException,是因为在客户端应用程序中,“对象”对象中“人”字段的值未初始化(它们没有分配值)。 You are trying to access a not initialized field of a Hibernate entity without being a Hibernate Session open and therefore Hibernate is not able to get that value. 您试图在未打开Hibernate会话的情况下访问Hibernate实体的未初始化字段,因此Hibernate无法获得该值。

As you said, you have to make sure you receive 'Object' instances with all their fields initialized. 如您所说,您必须确保收到的“对象”实例的所有字段均已初始化。 There are several possibilities depending on the way your Hibernate entities are mapped (It would be a good idea you provide us with the mappings to be able to answer you in a more specific way). 根据您的Hibernate实体的映射方式,有几种可能性(为您提供映射方式,以便能够以更具体的方式回答您是一个好主意)。 For instance, you could 例如,您可以

  1. Mark the relationship as eager so when you get the Object you get as well the Person (the way to do this depends on the way the mappings are declared) 将关系标记为渴望,这样当您获得Object时也会获得Person(完成此方法取决于声明映射的方式)
  2. Use a explicit join when you query the Object. 查询对象时,请使用显式联接。 Something like this in HQL (I don't know if you're using HQL either) HQL中类似这样的东西(我也不知道您是否也在使用HQL)

    select o from Object o join fetch o.owner where o.id = :id 从对象o中选择o加入o.owner,其中o.id =:id

Again if you provide us with more details, we could give a more accurate solution 同样,如果您向我们提供更多详细信息,我们可以提供更准确的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM