对于我们项目中的一个要求,我们遵循:
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 {
...
}
我发现很难为元数据表定义映射。
答案 0 :(得分:2)
根据规范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 {
...
}
答案 1 :(得分:0)
我的坏,我没有想到。这是最终的代码:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class AbstractEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name="TENANT_ID")
private int tenantId;
@OneToMany(mappedBy="entity",cascade=CascadeType.ALL,fetch = FetchType.EAGER)
private Set<ExtensionTable> extensionTable = new HashSet<ExtensionTable>();
.....
}
@Entity
@Table (name="metadata")
public class MetaData {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long metaDataId;
@Column
int tenantId;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="extensionId")
private ExtensionTable extension;
@Column
String extLabel;
@Column
String extDataType;
.....
}
@Entity
@Table(name = "extensionTable")
public class ExtensionTable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long extensionId;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="ENTITY_ID")
private AbstractEntity entity;
@OneToOne(cascade=CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name="metadata_id")
private MetaData metaDatas = new MetaData();
....
}