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?