gpt4 book ai didi

java - 无法从数据库中获取多对多关系中的对象集合

转载 作者:行者123 更新时间:2023-11-29 13:20:39 25 4
gpt4 key购买 nike

我在 Hibernate 中有以下层次结构:

@Entity
@Table(name = "elements")
@Inheritance(strategy=InheritanceType.JOINED)

public abstract class Elements implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idelement")
private Integer idElement;

@ManyToMany(cascade = CascadeType.PERSIST, mappedBy = "elementsCollection")
private Collection<ElementsGroups> elementsGroupsCollection;

.
.
More attributes, constructor and getter/setter
.
.
}

@Entity
@Table(name = "valves")
@PrimaryKeyJoinColumn(name="idelement")
@XmlRootElement
public class Valves extends Elements {

@Basic(optional = false)
@Column(name = "position")
private int position;

@Basic(optional = false)
@Column(name = "status")
private boolean status;

.
.
More attributes, constructor and getter/setter
.
.
}

@Entity
@Table(name = "elementsgroups")
@XmlRootElement
public class ElementsGroups implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idgroup")
private Integer idGroup;

@Basic(optional = false)
@Column(name = "description")
private String description;

@ManyToMany(cascade = {CascadeType.PERSIST})
@JoinTable(name="joinelementsgroups", joinColumns={@JoinColumn(name="idgroup")}, inverseJoinColumns={@JoinColumn(name="idelement")})
private Collection<Elements> elementsCollection;

.
.
More attributes, constructor and getter/setter
.
.
}

这个想法是有一个“元素”父类(super class)来包装系统的一堆元素,这些元素共享一些特性和功能。这些元素可以分为几组。这是数据库结构:

enter image description here

当我持久化 Valve 子类的对象并将其添加到一组时,我可以看到数据库中的更改。像这样:

    SessionFactory sf = new Configuration().configure().buildSessionFactory();

Session s = sf.openSession();
Transaction tx = s.beginTransaction();
Valve v1 = new Valve();
Valve v2 = new Valve();
s.save(v1);
s.save(v2);
tx.commit();

tx = s.beginTransaction();
LinkedList<Elements> valves = new LinkedList<>();
valves.add(v1);
valves.add(v2);
ge.setElementsCollection(valves);

s.save(ge);
tx.commit();

//Database: OK

当我获得 Valve 对象时,我可以通过它的 getter 方法获得该对象所属的组。当我尝试获取 ElementsGroups 对象时出现问题,我可以在其中获取它的所有属性,但是 elementsCollection 是空的而不是包含 Valve之前添加:

    tx = s.beginTransaction();
Valve v1 = (Valve)s.get(Valve.class, 3);
for(ElementsGroups g : v1.getElementsGroupsCollection())
System.out.println("Valve belongs to: " + g);

ElementsGroups ge = (ElementsGroups)s.get(ElementsGroups.class, 1);
System.out.println("Group number of elements: " + ge.getElementsCollection().size());

tx.commit();

输出:

Valve belongs to: model.ElementsGroups[ idgroup=1 ]
Group number of elements: 0

我已经实现了类似的关系,但没有显示这个问题,所以我猜这个问题可能与类的层次结构有关,但我是 Hibernate 的新手,不知道我是否缺少一些东西。

ps:为了方便阅读,我省略了部分代码,如有必要我可以添加

最佳答案

我想你忘了用 ElementGroups 实体填充 Valve 实体,这样双方就可以相互引用:

    tx = s.beginTransaction();
LinkedList<Elements> valves = new LinkedList<>();

Valve managedV1 = s.merge(v1);
Valve managedV2 = s.merge(v2);

valves.add(managedV1);
valves.add(managedV2);

managedV1.getElementsGroupsCollection().add(ge);
managedV2.getElementsGroupsCollection().add(ge);

ge.setElementsCollection(valves);

s.save(ge);
tx.commit();

此外,我认为您应该在第二笔交易中首先合并 Valves。

关于java - 无法从数据库中获取多对多关系中的对象集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42389153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com