Hi all
I'm trying to inject some unique constraints at runtime. I've looked around and I'm lost.... any help is appreciated.
I have the following setup:
hibernate entity manager + annotations 3.5.4-Final
the model looks like
Code:
@Entity(name="User")
public class UserDBImpl {
....
@Column(name="ATTRIBUTES", nullable=false)
@ElementCollection
@JoinTable(name="UserAttributes", joinColumns=@JoinColumn(name="username"))
@MapKeyColumn(name="attrkey")
private Map<String, String> attrs = new HashMap<String, String>();
...
so the UserAttributes table would look like:
username___________attrkey_________attrvalue
user1 attr1 foo
user2 attr1 bar
The goal is to add constraints such as "attrvalue should be unique if attrkey is 'attr1'" at runtime. By runtime, I mean any time before first database operation is requested.
I thought about custom validator but in all the examples the validator does not have access to other fields in the table. (same problem with @PostUpdate. can anyone give an example?)
I thought about ALTER TABLE, but it's a native query and I want to avoid it.
I was looking at EJB3Configuration class, hoping to make some calls to add unique constraint before an entity manager is built but I'm even more confused after looking at that class.
Any help is appreciated
thanks