I have a user ID identified with two natural IDs, something like
@ entity @ table (name = "user", uniqueConstraints = {@UniqueConstraint (COLUMNNAMES = "email"), @UniqueConstraint (COLUMNNAMES = "Nick")}) public user () {} @Id @GeneratedValue (strategy = Id) @ Column (name = "id", unique = true, tap = =) private ID; @column (name = "email", exclusive = true, zero = wrong, length = 31) @NaturalId (mutable = true) private string email; @Column (name = "nick", unique = true, tap = false, length = 31) @NaturalId (variable = false) private string Nick;
However, when I try to execute
using session.byNaturalId (User.class) .with (LockOptions.READ) (" Email ",." Admin@mail.com ") Load ();
This is an exception
throws org.hibernate.HibernateException: Unit [pervasive.com.gmail.tigerjack89.forum.shared.model.entities.User ] Defines its natural-id with 2 properties, but only specified in 1 org.hibernate.event.spi.ResolveNaturalIdEvent. & Lt; Init>. (ResolveNaturalIdEvent.java:75) at org.hibernate.event.spi.ResolveNaturalIdEvent & lt; Init & gt; On pervasive.com on org.hibernate.internal.SessionImpl $ BaseNaturalIdLoadAccessImpl.resolveNaturalId (SessionImpl.java:2607) on org.hibernate.internal.SessionImpl $ NaturalIdLoadAccessImpl.load (SessionImpl.java:2722) (ResolveNaturalIdEvent.java:52). on gmail.tigerjack89.forum.server.model.orm.StorageManager.getByNaturalId pervasive.com.gmail.tigerjack89.test.local.MyHibernateTest.test1 (MyHibernateTest.java:37) (StorageManager.java:217) on pervasive.com. Gmail.tigerjack89.test.local.MyHibernateTest.main (MyHibernateTest.java.2)
Why is that so? I think this is also due to the log of SQL syntax generated by Hibernate. In fact, this strange (unnecessary) is at this point and I think this is the reason for the exception
Hibernate: aLTER tABLE add user constraint UK_t8tbwelrnviudxdaggwr1kd9b unique (email, Nick) hibernate: changing table to add user hibernate barrier UK_ob8kqyqqgmefl0aco34akdtpe unique (email): aLTER tABLE barrier UK_pvnbxcf to add user ihb58o5n2n1fnc7fh1 unique (Nick)
Edit: code reading again, I thought that the problem could be related to the @ Unique Consultants
annotation However, even That if I try to remove one of them, the Hibernate above remains the syntax syntax genetate.
After
I will only create an email address with natural ID and then your query will work
When two columns are identified as natural IDs, then it creates a composite key.
Nick can still be used as a foreign key
Comments
Post a Comment