bornleo
bornleo

Reputation: 488

JPA + Hibernate Mapping for Extension table

For one of the requirements in our project we are following :

http://msdn.microsoft.com/en-us/library/aa479086.aspx#mlttntda_nvp to manage multiple tenants.

Please refer to above link for image of table structure.

I need help for managing the mapping. Here is what i am thinking:

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

I am finding it hard to define mapping for Metadata table.

Upvotes: 2

Views: 1089

Answers (2)

bornleo
bornleo

Reputation: 488

My bad, i didn't think it through. Here is the final code :

@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();

    ....

    }

Upvotes: 0

Eus777
Eus777

Reputation: 63

From the spec 11.1.26: The ManyToOne annotation defines a single-valued association to another entity class that has many-to-one multiplicity. I don't think you can use @ManyToOne with Set. I would try something like this:

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

Upvotes: 2

Related Questions