Hello, I'm trying to prototype creating a Polymorphic @Any relationship between some data. The technologies I'm using:
1) JavaEE 7 2) Spring 4.2.2.RELEASE (Core, MVC, Data,Persistence, ORM...) 3) Hibernate 4.2.20.FINAL 4) JPA 2 5) MySql 5.6
I'm going to be brief with the class description information for the sake of brevity.
For my Entities I have the following:
@Entity @DiscriminatorValue(value = "INDIVIDUAL") public class Individual implements IAssignee { @Id @Column(name = "uuid", unique = true, nullable = false, length = 37) private String uuid = ""; @Column(name = "firstName", unique = false, nullable = false, length = 64) private String firstName = ""; @Column(name = "lastName", unique = false, nullable = false, length = 64) private String lastName = ""; @Column(name = "name", unique = false, nullable = false, length = 128) private String name = ""; ... }
@Entity @DiscriminatorValue(value = "UGROUP") public class UserGroup implements IAssignee { @Id @Column(name = "uuid", unique = true, nullable = false, length = 37) private String uuid = ""; @Column(name = "name", unique = false, nullable = false, length = 128) private String name = ""; ... }
public interface IAssignee { String getUuid(); String getName(); }
@Entity public class WorkProduct { @Id @Column(name = "id", unique = true, nullable = false) private int id = 0;
@Column(name = "name", unique = true, nullable = false, length = 37) private String name = "";
@Any(metaColumn = @Column(name = "assigneeType")) @AnyMetaDef(idType = "string", metaType = "string", metaValues = { @MetaValue(targetEntity = Individual.class, value = "INDIVIDUAL"), @MetaValue(targetEntity = UserGroup.class, value = "UGROUP") }) @Cascade({ org.hibernate.annotations.CascadeType.ALL }) @JoinColumn(name = "assigneeUuid", referencedColumnName = "uuid") private IAssignee assignee;
}
Here is the SQL table descriptions:
CREATE TABLE `individual` ( `uuid` varchar(37) NOT NULL, `firstName` varchar(64) NOT NULL, `lastName` varchar(64) NOT NULL, `name` varchar(128) NOT NULL, PRIMARY KEY (`uuid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `usergroup` ( `uuid` varchar(37) NOT NULL, `name` varchar(128) NOT NULL, PRIMARY KEY (`uuid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `workproduct` ( `id` int(11) NOT NULL, `name` varchar(37) NOT NULL, `assigneeType` int(11) NOT NULL, `assigneeUuid` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Example of data in Individual table:
uuid firstName lastName name ------------------------------------------------- "123" "john" "smith" "jsmith"
Example of data in UserGroup table: uuid name ------------------------ "456" "Analyst"
Example of data in WorkProduct table
ID Name AssigneeType AssigneeUUID ------------------------------------------------------ 1 productA 1 "123" 2 productB 2 "456"
So here's the issues, the AssigneeType ID is an integer value from my assigneeType table
id value -------------- 1 "INDIVIDUAL" 2 "UGROUP"
My @Any relationship in WorkProduct
@Any(metaColumn = @Column(name = "assigneeType")) @AnyMetaDef(idType = "string", metaType = "string", metaValues = { @MetaValue(targetEntity = Individual.class, value = "INDIVIDUAL"), @MetaValue(targetEntity = UserGroup.class, value = "UGROUP") }) @Cascade({ org.hibernate.annotations.CascadeType.ALL }) @JoinColumn(name = "assigneeUuid", referencedColumnName = "uuid") private IAssignee assignee;
is not working because assigneeType column is an integer value. Can someone help me fix my @Any relationship declaration?
thanks, mbeddedsoft
|