JPA 实体管理器问题

发布于 2024-11-17 10:37:31 字数 1692 浏览 3 评论 0原文

我对 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 技术交流群。

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

发布评论

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

评论(2

烛影斜 2024-11-24 10:37:31

当双向关系的双方不一致时,拥有方(即没有mappedBy的一方)的状态将被持久化到数据库中。因此,您需要一致地修改双方:

for (Part p: corolla.getParts()) {
    p.setCar(null);
}
corolla.getParts().clear();

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:

for (Part p: corolla.getParts()) {
    p.setCar(null);
}
corolla.getParts().clear();
倾城°AllureLove 2024-11-24 10:37:31

它看起来不错,但是在从数据库中删除实体时,您应该使用:

EntityManager.remove

It looks fine, however while removing an entity from your database you should be using:

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