Hibernate: enforcing unique data members
I am having an issue working with Hibernate and enforcing unique data members when inserting.
Here are my abridged Entity objects:
Workflow:
@Entity
public class Workflow {
private long wfId;
private Set<Service> services;
/** Getter/Setter for wfId */
...
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "workflow_services",
joinColumns = @JoinColumn(name = "workflow_id"),
inverseJoinColumns = @JoinColumn(name = "service_id"))
public Set<Service> getServices() {
return services;
}
Service:
@Entity
public class Service {
private long serviceId;
private String serviceName;
/** Getter/Setter for serviceId */
...
@Column(unique=true,nullable=false)
public String getServiceName() {
return serviceName;
}
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "service_operations",
joinColumns = { @JoinColumn(name = "serviceId") },
inverseJoinColumns = { @JoinColumn(name = "operationId") })
public Set<Operation> getOperations() {
return operations;
}
Operation:
@Entity
public class Operation {
private long operationId;
private String operationName;
/** Getter/Setter for operationId */
@Column(unique=true,nullable=false)
public String getOperationName() {
return operationName;
}
My issue:
Although I have stated in each object what is SUPPOSED to be unique, it is not being enforced.
Inside my Workflow object, I maintain a Set of Services. Each Service maintains a list of Operations. When a Workflow is saved to the database, I need it to check if the Services and Operations it currently uses are already in the database, if so, associate itself with those rows.
Currently I am getting repeats within my S开发者_Python百科ervices and Operations tables.
I have tried using the annotation: @Table( uniqueConstraints)
but have had zero luck with it.
Any help would be greatly appreciated
The unique or uniqueConstraints attributes are not used to enforce the uniqueness in the DB, but create the correct DDL if you generate it from hibernate (and for documentation too, but that's arguable).
If you declare something as unique in hibernate, you should declare it too in the DB, by adding a constraint.
Taking this to the extreme, you can create a mapping in which the PK is not unique in the DB, and hibernate will throw an exception when it tries to load one item by calling Session.load, and sudently finding that there are 2 items.
Inside my Workflow object, I maintain a Set of Services. Each Service maintains a list of Operations. When a Workflow is saved to the database, I need it to check if the Services and Operations it currently uses are already in the database, if so, associate itself with those rows.
I think you're asking Hibernate to detect duplicate objects when you add them to the Set, yes? In other words, when you put an object in the Set, you want Hibernate to go look for a persistent version of that object and use it. However, this is not the way Hibernate works. If you want it to "reuse" an object, you have to look it up yourself and then use it. Hibernate doesn't do this.
I would suggest having a helper method on a DAO-like object that takes the parent and the child object, and then does the lookup and setting for you.
精彩评论