1

I just refactor a Project to use Hibernate (4.2.4.Final) with Inheritance. But I got trouble with ManyToMany annotation.

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.

@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.

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*.

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.

Any suggestions how to solve this?

2 Answers 2

1

I just found the source of the Problem.

It was a missing/wrong equals method. :-)

0

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.

In my opinion it is the reason of your problem.

1
  • No, also when I switch to a @ManyToMany this problem still exists.
    – Johannes
    Commented Aug 22, 2013 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.