无法从数据库

时间:2017-02-22 10:51:40

标签: java postgresql hibernate jpa

我在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
    .
    .
}

这个想法是拥有一个“Elements”超类来包装系统的一些元素,这些元素共享一些特征和功能。这些元素可以分为几组。这是数据库结构:

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:请注意,我将代码的几个部分下拉以方便阅读,但我可以在必要时添加

1 个答案:

答案 0 :(得分:1)

我认为您忘记使用ElementGroups实体填充Valve实体,以便双方都有相互引用:

Error: Unexpected value 'MyModule' imported by the module 'class1' at SyntaxError.ZoneAwareError

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