-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Enforcing unique data members
PostPosted: Fri Sep 30, 2011 2:06 pm 
Newbie

Joined: Fri Sep 16, 2011 6:16 pm
Posts: 3
Greetings, I am having an issue working with Hibernate and enforcing unique data members when inserting.

Here are my abridged Entity objects:


Workflow:

Code:
@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:

Code:
@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:

Code:
@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 Services and Operations tables.

I have tried using the annotation:
Code:
@Table( uniqueConstraints)


but have had zero luck with it.

Any help would be greatly appreciated


Top
 Profile  
 
 Post subject: Re: Enforcing unique data members
PostPosted: Sun Oct 02, 2011 5:02 pm 
Newbie

Joined: Sun Sep 25, 2011 4:05 pm
Posts: 12
1. Have you tried with a fresh database? Meaning you did drop database, recreate, and then run your test? You let hibernate create the database right?
2. Can you post the output from sql of "show create table X;" where X is the name of each of your tables? Alternatively try "describe X". Make sure the unique constraint has made its way onto the table definition. Sometimes when you change your annotations, hibernate cannot update the database so you have to drop and let hibernate recreate it.
3. I don't think there is any method in hibernate to find an entity in the database which your transient instance would conflict with. There is no function that finds a persistent instance that has the same unique values as the current transient instance (to my knowledge anyways). I wound up writing a function named "loadExistingOrSave()". First it would find a persistent instance using a query on the unique fields that equal my transient instance's unique fields. If it found one, that is returned. If it did not find one, it would save the current transient instance.

Other advice:
Just a suggestion, might want to mark your classes @Immutable if they are.


Top
 Profile  
 
 Post subject: Re: Enforcing unique data members
PostPosted: Mon Oct 03, 2011 9:27 am 
Newbie

Joined: Fri Sep 16, 2011 6:16 pm
Posts: 3
I have been successful in rooting out duplicates up until a point.

At this point, everything works great until I come across as Workflow that uses a service that already exists within the database.

This is my annotation I use to generate my Join Table.

Code:
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "service_operations",
        joinColumns = { @JoinColumn(name = "serviceId") },
        inverseJoinColumns = { @JoinColumn(name = "operationId") })
public Set<Operation> getOperations() {
    return operations;
}


And this is the resulting table
Code:
mysql> describe workflow_services;
+-------------+------------+------+-----+---------+-------+
| Field       | Type       | Null | Key | Default | Extra |
+-------------+------------+------+-----+---------+-------+
| workflow_id | bigint(20) | NO   | PRI | NULL    |       |
| service_id  | bigint(20) | NO   | PRI | NULL    |       |
+-------------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)


Should I change my @OneToMany to a @ManyToMany so that I can get repeats in to the second column of my join?

Example:
Code:
+-------------+------------+
| workflow_id | Service_id |
+-------------+------------+
|       1     |        1   |
+-------------+------------+
|       3     |        1   |
+-------------+------------+


i.e. See how the service_id has a repeated value?? I am looking for that to happen in my Join table. What I believe I want is a

workflow_id + service_id COMPOSITE key where service_id is NOT Primary.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.