简体   繁体   English

使用继承时,Hibernate ManyToMany不起作用

[英]Hibernate ManyToMany does not work when using Inheritance

I just refactor a Project to use Hibernate (4.2.4.Final) with Inheritance. 我只是重构一个项目以将Hibernate(4.2.4.Final)与继承一起使用。 But I got trouble with ManyToMany annotation. 但是我对ManyToMany注释感到麻烦。

I have a base File Class like this: 我有一个像这样的基本文件类:

@Entity
@Table(name = "file")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "descriminator", length = 25)
public abstract class File {

    @Id
    @Column(name = "id", unique = true, nullable = false, length = 256)
    private String id;
}

and a special Inheritance class like this: 和一个特殊的继承类,如下所示:

@Entity
@DiscriminatorValue("ISSUE_HISTORY_ATTACHMENT")
@Data
public class IssueHistoryAttachment extends File {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinTable(name = "issue_history_attachment", joinColumns = {
            @JoinColumn(name = "attachment_id", nullable = false, unique = true) }, inverseJoinColumns = {
            @JoinColumn(name = "issue_history_id", nullable = false)})
    private IssueHistory history;

}

This IssueHistoryAttachment Class is also referenced in my IssueHistory Class. 我的IssueHistory类中也引用了此IssueHistoryAttachment类。

@Entity
@Table(name = "issue_history")
@TableGenerator(name="tg", table="hibernate_sequences",pkColumnName="sequence_name", valueColumnName="sequence_next_hi_value", allocationSize=1)
public class IssueHistory implements java.io.Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "tg")
    @Column(name = "id", unique = true, nullable = false)
    private int id;

// some other fields

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "issue_history_attachment", joinColumns = {
            @JoinColumn(name = "issue_history_id", nullable = false)
        }, inverseJoinColumns = {
            @JoinColumn(name = "attachment_id", nullable = false, unique = true)
    })
    private Set<IssueHistoryAttachment> attachments = new HashSet<IssueHistoryAttachment>();

}

When i now store a IssueHistory Instance with two Attachments, all this fields are correctly saved in my database. 当我现在存储带有两个附件的IssueHistory实例时,所有这些字段都正确保存在我的数据库中。

I got 2 new entries in the file table, one new entry in the *issue_history* table and two correct entries in the relation table *issue_history_attachment*. 我在文件表中有2个新条目,在* issue_history *表中有一个新条目,在关系表* issue_history_attachment *中有两个正确的条目。

So at this points all thinks are looking fine. 因此,在这一点上,所有想法都看起来不错。 But when I try to read the Values Attachment Set in the IssueHistory Instance only contains one element instead of two like stored in the database. 但是,当我尝试读取IssueHistory实例中的“值附件集”时,它仅包含一个元素,而不是存储在数据库中的两个元素

Any suggestions how to solve this? 有什么建议如何解决这个问题?

I just found the source of the Problem. 我刚刚找到问题的根源。

It was a missing/wrong equals method. 这是一种缺失/错误的equals方法。 :-) :-)

I can't comment yes so I have to make an answer. 我不能说是,所以我必须回答。

I see one problem in your code (or maybe I don't understand it): 我在您的代码中看到一个问题(或者也许我听不懂):

In IssueHistory you are using @ManyToMany to IssueHistoryAttachment but in IssueHistoryAttachment you are using @ManyToOne . IssueHistory您使用@ManyToManyIssueHistoryAttachment但是在IssueHistoryAttachment您使用@ManyToOne

In my opinion it is the reason of your problem. 我认为这是您遇到问题的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM