I have some Hibernate/JPA annotations (still don't know the difference to be honest) that are allowing me to create an association class. This class is combining two items that are related into one object. I originally was using @JoinTable
but realized I needed a lot more meta data with the associations, so had to convert the code over into another object type.
For now I am using @Id
to mark the ID
column for my objects, and using @NaturalId (mutable = false)
for a String uuid
.
My association class is using @ManyToOne
and creates the table just fine, but when I look into it the table is using the @Id
field as the mapping column. I would prefer to have this association class use the @NaturalId uuid
for ease of transferring relationship/associations across to other systems.
How can I get the relationship to use the correct identifier?
For reference, my DB's and Java code look like this:
AssociationsTable
----------------------------------------------
| ID | META DATA | ID ASSOC. 1 | ID ASSOC. 2 |
----------------------------------------------
| 1 | stuff | 1 | 2 |
----------------------------------------------
Objects
------------------------------
| ID | META DATA | UUID |
------------------------------
| 1 | stuff | FOO-123 |
------------------------------
| 2 | stuff | BAR-456 |
------------------------------
Association Class{
ObjA main;
ObjA sub;
@ManyToOne
getMain()
@ManyToOne
getSub()
}
ObjA Class{
Long id;
@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
@XmlElement
@Column(name = "ID", unique = true, nullable = false)
getId()
String uuid;
@NaturalId (mutable = false)
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "uuid", unique = true)
getUUID()
}
@Id
, then they can be serialized with reset key values, then transmitted to other instance, deserialized with all relations at java-bean level, then flushed in new database with new values for@Id
properties... Primary keys for entities will be different but you will still get them by@NaturalId
field. Did I miss something?.. – Modla