我只是重构一个项目以使用带有继承的 Hibernate (4.2.4.Final)。但是我在使用 ManyToMany 注释时遇到了麻烦。
我有一个像这样的基本文件类:
@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;
}
和一个像这样的特殊继承类:
@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;
}
这个 IssueHistoryAttachment 类也在我的 IssueHistory 类中被引用。
@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>();
}
当我现在存储带有两个附件的 IssueHistory 实例时,所有这些字段都正确保存在我的数据库中。
我在文件表中有 2 个新条目,*issue_history* 表中有一个新条目,关系表 *issue_history_attachment* 中有两个正确条目。
所以在这一点上,所有的想法看起来都很好。但是当我尝试读取 IssueHistory 实例中的 Values Attachment Set 时,它只包含一个元素,而不是存储在数据库中的两个元素。
任何建议如何解决这个问题?