JPA 2.0 提供者休眠

发布于 2024-10-09 09:30:53 字数 1187 浏览 0 评论 0原文

我有一个非常奇怪的问题,我们正在使用基于 hibernate 注释的 jpa 2.0 通过JPA DDL生成的数据库为true,数据库为MySQL;

我将提供一些参考课程,然后是我的问题。

@MappedSuperclass
public abstract class Common implements serializable{
 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 @Column(name = "id", updatable = false)
 private Long id;

 @ManyToOne
 @JoinColumn
 private Address address;
        //with all getter and setters
        //as well equal and hashCode

}

@Entity
public class Parent extends Common{
         private String name;
         @OneToMany(cascade = {CascadeType.MERGE,CascadeType.PERSIST}, mappedBy = "parent") 
         private List<Child> child;
         //setters and rest of class
}

@Entity
public class Child extends Common{
//some properties with getter/setters
}

@Entity
public class Address implements Serializable{

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 @Column(name = "id", updatable = false)
 private Long id;

       private String street;
      //rest of class with get/setter

}

如代码中所示,您可以看到父类和子类扩展了 Common 类,因此都具有地址属性和 id ,当更改父类中的地址引用时会出现问题,它反映了列表中所有子对象的相同更改,并且如果更改子类中的地址引用类然后在合并时它也会更改父级的地址引用,

我无法弄清楚这是 jpa 还是 hibernate 的问题

I have very strange problem we are using jpa 2.0 with hibernate annotations based
Database generated through JPA DDL is true and MySQL as Database;

i will provide some reference classes and then my porblem.

@MappedSuperclass
public abstract class Common implements serializable{
 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 @Column(name = "id", updatable = false)
 private Long id;

 @ManyToOne
 @JoinColumn
 private Address address;
        //with all getter and setters
        //as well equal and hashCode

}

@Entity
public class Parent extends Common{
         private String name;
         @OneToMany(cascade = {CascadeType.MERGE,CascadeType.PERSIST}, mappedBy = "parent") 
         private List<Child> child;
         //setters and rest of class
}

@Entity
public class Child extends Common{
//some properties with getter/setters
}

@Entity
public class Address implements Serializable{

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 @Column(name = "id", updatable = false)
 private Long id;

       private String street;
      //rest of class with get/setter

}

as in code you can see that parents and child classes extends Common class so both have address property and id , the problem occurs when change the address refference in parent class it reflect same change in all child objects in list and if change address refference in child class then on merge it will change address refference of parent as well

i am not able to figure out is it is problem of jpa or hibernate

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

臻嫒无言 2024-10-16 09:30:53

如果您有共享的 Address 实例,则在子对象的“范围”内所做的更改确实会影响父对象,因为您正在处理父对象中的相同 Address 实例。

例如:

Parent1.address => Address #1
Child1.address => Address #2
Child2.address => Address #2
Child3.address => Address #1

在本例中,如果更改 Child3.address.street,则意味着它也更改了 Parent1.address.street。请注意,使 Parent1 和 Child3 中的地址相同的是 ID。如果它们拥有相同的 ID,则它们是相同的实例(即:两个对象之间“共享”)。

If you have shared instances of Address, changes made when it's in the "scope" of a child does affects the parent, because you are dealing with the same Address instance in the parent object.

For instance:

Parent1.address => Address #1
Child1.address => Address #2
Child2.address => Address #2
Child3.address => Address #1

In this case, if you change Child3.address.street, it means that it also changed Parent1.address.street. Note that what makes Address in Parent1 and Child3 the same is the ID. If they hold the same ID, they are the same instance (ie: "shared" among both objects).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文