对于我们项目中的其中一项要求,我们遵循:
http://msdn.microsoft.com/en-us/library/aa479086.aspx#mlttntda_nvp管理多个租户。
表结构图片请引用上面的链接。
我需要帮助来管理映射。这是我的想法:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class AbstractEntity {
@Id @GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
@OneToMany(mappedBy="entities")
private Set<ExtensionTable> extensionTable = new HashSet<ExtensionTable>();
....
}
@Entity
public class ExtensionTable {
@Id @GeneratedValue
private Long id;
@ManyToOne
private Set<AbstractEntity> entities = new HashSet<AbstractEntity>();
...
}
@Entity
public class Employee extends AbstractEntity {
...
}
我发现很难定义元数据表的映射。
最佳答案
来自规范 11.1.26:ManyToOne 注释定义与另一个具有多对一多重性的实体类的单值关联。 我认为您不能将 @ManyToOne 与 Set 一起使用。 我会尝试这样的事情:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) // Depending on your provider, I generally use JOINED or SINGLE_TABLE
public abstract class AbstractEntity {
@Id @GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
@OneToMany(mappedBy="entity")
private Set<ExtensionTable> extensionTable = new HashSet<ExtensionTable>();
....
}
@Entity
public class ExtensionTable {
@Id @GeneratedValue
private Long id;
@ManyToOne
private AbstractEntity entity;
@ManyToOne
private Metadata metadata;
@Column
private String value;
...
}
@Entity
public class Metadata {
@Id @GeneratedValue
private Long id;
@ManyToOne
private AbstractEntity entity;
@Column
private String extLabel;
@Column
private String extDataType; // So, here is a problem, you'll have to find an efficient way to deal with the data type.
...
}
@Entity
public class Employee extends AbstractEntity {
...
}
关于hibernate - JPA + Hibernate 扩展表映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15485810/