一对多双向性能问题
我有一个疑问,那就是当我们将一对多创建为双向时。我们将在子类中放置一个父类引用。
参见代码。
Person.java
@Entity
@Table(name="PERSON")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="personId")
private int id;
@Column(name="personName")
private String name;
@OneToMany(cascade =CascadeType.ALL,fetch = FetchType.LAZY)
@JoinColumn(name="personId")
private Set <Address> addresses;
....
....
}
Address.java
@Entity
@Table(name = "ADDRESS")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "addressId")
private int id;
@Column(name = "address",nullable=false)
private String address;
@ManyToOne(cascade =CascadeType.ALL)
@JoinColumn(name="personId",nullable=false)
private Person person;
....
....
}
这里 Person.java 是父类,Address.java 是子类。 当我们从数据库中获取父对象时,它正在加载子类。没关系。没问题。
但反之亦然,即。如果我们要获取子类,它也会保存父类(person)。
例如:我们通过地址 id 获取地址。假设从数据库中检索到 100 个地址。
但在地址类 person 变量中也保存父(Person)对象。
我的疑问是
使用人的不同的100个记忆。具有相同的数据。在地址类中。 ?
我的道就是这样的。
public List<Address> getAllAddressByPersonId(int personId) {
List<Address> addressList = null;
try {
DetachedCriteria criteria = DetachedCriteria.forClass(Address.class);
criteria.createCriteria("person").add(Restrictions.eq("id", personId));
addressList = getHibernateTemplate().findByCriteria(criteria);
} catch (HibernateException e) {
e.printStackTrace();
} catch (DataAccessException e) {
e.printStackTrace();
}
return addressList;
假设 addrssList 的大小为 100。
就像
per = addressList.get(0).getPerson() ,
per 1 = addressList.get(1).getPerson(),
per2 = addressList.get(2).getPerson(),
....
per99 = addressList.get(99).getPerson().
这里 per1、per2 .... 和 per 99 使用相同或不同的内存。
如果相同就可以了..否则可能会导致更多内存利用率的任何执行问题。?
请帮助...
i have one doubt, that is when we create one-to-many as bidirectional. we will put one parent class reference in child class.
see the code.
Person.java
@Entity
@Table(name="PERSON")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="personId")
private int id;
@Column(name="personName")
private String name;
@OneToMany(cascade =CascadeType.ALL,fetch = FetchType.LAZY)
@JoinColumn(name="personId")
private Set <Address> addresses;
....
....
}
Address.java
@Entity
@Table(name = "ADDRESS")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "addressId")
private int id;
@Column(name = "address",nullable=false)
private String address;
@ManyToOne(cascade =CascadeType.ALL)
@JoinColumn(name="personId",nullable=false)
private Person person;
....
....
}
here Person.java is parent class and Address.java is child class.
when we fetch the parent object from data base it is loading child class. it is fine. no proble.
but in vice versa ie. if we are fetching child class it is also holding parent class (person).
eg: we fetch Address by address id. just assume retrieved 100 address from data base.
but in address class person variable hold the parent(Person) object aslo.
my doubt is here
is use different 100 memory of person. with same data. in Address class. ?
my dao is like this.
public List<Address> getAllAddressByPersonId(int personId) {
List<Address> addressList = null;
try {
DetachedCriteria criteria = DetachedCriteria.forClass(Address.class);
criteria.createCriteria("person").add(Restrictions.eq("id", personId));
addressList = getHibernateTemplate().findByCriteria(criteria);
} catch (HibernateException e) {
e.printStackTrace();
} catch (DataAccessException e) {
e.printStackTrace();
}
return addressList;
just assume the size of addrssList is 100.
like
per = addressList.get(0).getPerson() ,
per 1 = addressList.get(1).getPerson(),
per2 = addressList.get(2).getPerson(),
....
per99 = addressList.get(99).getPerson().
here per1, per2 .... and per 99 are using same memory or different.
if same it is ok.. else it may cause of any performing issue of more memory utilization.?
help pls...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Hibernate 进行身份检查。一个 Hibernate 会话仅包含具有相同 ID 的对象的单个实例。因此,如果 per、per1、...per99 是一个人,那么您将拥有一个对象。否则 - 不同的对象。
Hibernate does identity check. One hibernate session contains only single instance of an object with the same ID. So, if per, per1,...per99 is one person, you will have one object for it. Otherwise - different objects.