JPA 实体管理器问题
我对 JPA/Hibernate 和 Java 都很陌生,并且在使用 EntityManager 类管理持久对象的基础知识方面遇到了一些麻烦。如果有人能向我解释一些非常基本的东西,我将非常感激,因为我无法从文档中弄清楚。
JSE 环境中的 JPA 2 / Hibernate / Postgresql。
* 下面的类定义*
以下内容按我的预期工作:
em.getTransaction().begin();
Car corolla = new Car();
Part clutch = new Part();
clutch.setCar( corolla );
Part bumper = new Part();
bumper.setCar( corolla );
em.persist( corolla );
em.persist( clutch );
em.persist( bumper );
em.getTransAction().commit();
但这不会删除从零件到数据库中汽车的链接:
tx.begin();
corolla.getParts().clear();
tx.commit();
为什么会出现这种情况?
提前致谢,如果这是一个愚蠢的问题,我很抱歉。
麦克风。
汽车类别:
@Entity
public class Car {
private Long id;
private Set<Part> parts;
....
public Car() { parts = new HashSet<Part>(); }
@Id
@GeneratedValue( generator="increment" )
@GenericGenerator( name="increment", strategy = "increment" )
public Long getId() { return id; }
private void setId( Long id ) { this.id = id; }
@OneToMany( mappedBy="car", cascade=CascadeType.ALL )
public Set<Part> getParts() { return this.parts; }
public void setParts( Set<Part> parts ) { this.parts = parts; }
....
}
零件类别:
@Entity
public class Part {
private Long id;
private Car car;
...
public Part() {};
@Id
@GeneratedValue( generator="increment" )
@GenericGenerator( name="increment", strategy = "increment" )
public Long getId() { return id; }
private void setId( Long id ) { this.id = id; }
@ManyToOne
@JoinColumn( name="car_id" )
public Car getCar() { return this.car; }
public void setCar( Car car ) { this.car = car; }
...
}
I'm quite new to both JPA/Hibernate and to Java, and am having some trouble with the basics of using the EntityManager class to manage persistent objects. I would appreciate it very much if someone would explain something very basic to me, because I can't figure it out from the documentation.
JPA 2 / Hibernate / Postgresql in a JSE environment.
* Class definitions below *
The following works as I would expect it to:
em.getTransaction().begin();
Car corolla = new Car();
Part clutch = new Part();
clutch.setCar( corolla );
Part bumper = new Part();
bumper.setCar( corolla );
em.persist( corolla );
em.persist( clutch );
em.persist( bumper );
em.getTransAction().commit();
But this does not remove the links from the parts to the cars in the database:
tx.begin();
corolla.getParts().clear();
tx.commit();
Why is this the case?
Thanks in advance, and sorry if this is a silly question.
Mike.
Car class:
@Entity
public class Car {
private Long id;
private Set<Part> parts;
....
public Car() { parts = new HashSet<Part>(); }
@Id
@GeneratedValue( generator="increment" )
@GenericGenerator( name="increment", strategy = "increment" )
public Long getId() { return id; }
private void setId( Long id ) { this.id = id; }
@OneToMany( mappedBy="car", cascade=CascadeType.ALL )
public Set<Part> getParts() { return this.parts; }
public void setParts( Set<Part> parts ) { this.parts = parts; }
....
}
Part class:
@Entity
public class Part {
private Long id;
private Car car;
...
public Part() {};
@Id
@GeneratedValue( generator="increment" )
@GenericGenerator( name="increment", strategy = "increment" )
public Long getId() { return id; }
private void setId( Long id ) { this.id = id; }
@ManyToOne
@JoinColumn( name="car_id" )
public Car getCar() { return this.car; }
public void setCar( Car car ) { this.car = car; }
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当双向关系的双方不一致时,拥有方(即没有
mappedBy
的一方)的状态将被持久化到数据库中。因此,您需要一致地修改双方:When sides of bidirectional relationship disagree, a state of the owning side (i.e. a side without
mappedBy
) is persisted to the database. So, you need to modify both sides consitently:它看起来不错,但是在从数据库中删除实体时,您应该使用:
It looks fine, however while removing an entity from your database you should be using: