2

对于我们项目中的要求之一,我们遵循:

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 {
    ...
}

我发现很难为元数据表定义映射。

4

2 回答 2

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 {
    ...
}
于 2013-03-19T16:03:47.843 回答
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();

    ....

    }
于 2013-03-19T23:48:46.847 回答