-->
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.  [ 13 posts ] 
Author Message
 Post subject: Naming constraints
PostPosted: Tue May 27, 2008 10:22 am 
Newbie

Joined: Tue May 27, 2008 10:11 am
Posts: 7
Hello,

In Hibernate documentation I found that: "Currently, the specified value of the unique-key attribute is not used to name the constraint in the generated DDL". But is it possible to name unique constraint?

I like to catch ConstraintViolationException and from it get to know which constraint was violated - is it userId, is it userAlternateLogin or something else. It would be great to now it from exception. As a workaround I catch this exception and then I check if there is userId already used or maybe userAlternateLogin is already used or... But it is inconvenient.

Thanks for any help with this issue.

P.S. I've searched documentation, googled many pages and I did not find the answer.


Top
 Profile  
 
 Post subject: re:naming constraints
PostPosted: Tue May 27, 2008 10:38 am 
Beginner
Beginner

Joined: Fri Jun 29, 2007 11:12 am
Posts: 25
Hello,
after hibernate throws a runtime exceptions, session becomes non-usable, you have to roll back the transaction and also close current session. ConstraintViolationException is wrapped in a RuntimeException. Exceptions thrown by hibernate are considered fatal, you would see the message though telling you which constraint was violated.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 10:50 am 
Newbie

Joined: Tue May 27, 2008 10:11 am
Posts: 7
But in ConstraintViolationException is method "getConstraintName". For me it returns null, so I thought that I need to name unique constraints in hbm's. But I do not know how to do it.

Also I thought that it is good strategy to try to save something and then catch for constraints exceptions as opposed to check manually if for example userId does not exist and then if not save something. Am I wrong?


Top
 Profile  
 
 Post subject: re:naming constrint
PostPosted: Tue May 27, 2008 1:33 pm 
Beginner
Beginner

Joined: Fri Jun 29, 2007 11:12 am
Posts: 25
Hello,
when you persist use saveOrUpdate() method, hibernate will figure out whether to update or save. In case you do not want to update, try to retrieve it first. eg:

two choices:

Code:
...
User user = (User) load(User.class, userId); // if does not exist - throws
                  //checked ObjectNotFoundException, which you can handle

User user = (User) load(User.class, userId); // if does not exist returns
                                                                //null,
...


Its a standard behaviour in hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 2:47 pm 
Newbie

Joined: Tue May 27, 2008 10:11 am
Posts: 7
Thans emiakoup for trying to help. This partially solves my problem. But in general my problem is little more complicated.

Suppose we have User object which have:
firstName, lastName,
employeeId (must be unique),
policyId (must be unique),
boxNumber (must be unique).

No we create User object and trying to save it. No we have at least two options:
1. check if there is no user having such employeeId, check if there is no user with such policyId, check if there is no user with such boxNumber and then if all is ok, save User
2. save User object and catch exceptions e.g. ConstraintViolationException and from it (using getConstraintName() method) get know which constraint was violated.

The problem with option one is too many checks, too many sql queries to db. Option two is much better (expecially when constraint violation is supposed to be very rare) but... and here is my problem - I do not now how to set up constraint name in hbm's, so these name would be set in database and getConstraintName() method (or other not known by me) returns these name. I tried to catch ConstraintViolationException and it catches but getConstraintName returns null.

Personally I think that option two is better but please help me know how to get which constraint was violated.


Top
 Profile  
 
 Post subject: re:naming constrints
PostPosted: Tue May 27, 2008 5:06 pm 
Beginner
Beginner

Joined: Fri Jun 29, 2007 11:12 am
Posts: 25
Hello,
here is what i found, this will actually name the constraint:

Code:
...
<database-object>
       <create>
              alter table USER add constraint UNIQUE_EMPLOYEE_ID
              unique (EMPLOYEE_ID);
      </create>
      <drop>
             drop constraint UNIQUE_SIBLINGS
      </drop>
</database-object>
...


try it.


Top
 Profile  
 
 Post subject: Naming constraints
PostPosted: Fri Mar 20, 2009 11:44 am 
Newbie

Joined: Fri Mar 20, 2009 11:40 am
Posts: 4
How to name the constraint using Hibernate Annotations? Any clue?

Thanks in advance.


Top
 Profile  
 
 Post subject: re:constraint with annotation
PostPosted: Fri Mar 20, 2009 12:12 pm 
Beginner
Beginner

Joined: Fri Jun 29, 2007 11:12 am
Posts: 25
Hello With annotations (you need hibernate as JPA), you do something like the following:

Code:
@Entity
@org.hibernate.annotations.Check(
constraints = "START_DATE < END_DATE"
)
public class Item { ... }


and for separate column:

Code:
@Column(name = "USERNAME", length = 16,
nullable = false, unique = true)
@org.hibernate.annotations.Check(
constraints = "regexp_like(USERNAME,'^[[:alpha:]]+$')"
)
private String username;


Top
 Profile  
 
 Post subject: Naming constraints
PostPosted: Fri Mar 20, 2009 12:33 pm 
Newbie

Joined: Fri Mar 20, 2009 11:40 am
Posts: 4
Hi, thanks emiakoup for trying to help. Actually, I want to know how to name the constraint using Hibernate Annotations. In your example you were able to define a constraint using Hibernate Annotation, but there is no name for it.


Top
 Profile  
 
 Post subject: re:constraint name
PostPosted: Fri Mar 20, 2009 1:21 pm 
Beginner
Beginner

Joined: Fri Jun 29, 2007 11:12 am
Posts: 25
Hello JaVaSan,
here is something i found:

Code:
@Entity
public class Child {
    ...
    @ManyToOne
    @ForeignKey(name="FK_PARENT")
    public Parent getParent() { ... }
    ...
}


Top
 Profile  
 
 Post subject: Naming constraints
PostPosted: Sat Mar 21, 2009 6:05 pm 
Newbie

Joined: Fri Mar 20, 2009 11:40 am
Posts: 4
Hello emiakoup,
Thank you for the point. I didn't know about @ForeignKey annotation.
I think we can't name constraints like these:

Code:
@Entity
public class User {
   
   ...
   @Column(nullable = false, unique = true)
   private String login;
   ...
   
}


Note that we have two constraints (nullable and unique), but there's no way to name them. Hibernate don't allow us to name them because it's not possible. Take a look at this DDL (based on MySQL database):

Code:
create table user (
    ...
    login varchar(255) not null unique
    ...
)


Note that there's no way to name the "not null" and "unique" constraints.


Top
 Profile  
 
 Post subject: RE:NAMING CONSTRAINTS
PostPosted: Mon Mar 23, 2009 9:51 am 
Beginner
Beginner

Joined: Fri Jun 29, 2007 11:12 am
Posts: 25
next post... (submitted twice


Last edited by emiakoup on Mon Mar 23, 2009 9:58 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: RE:NAMING CONSTRAINTS
PostPosted: Mon Mar 23, 2009 9:53 am 
Beginner
Beginner

Joined: Fri Jun 29, 2007 11:12 am
Posts: 25
Hello JaVaSan,
raise a feature request in JIRA. Once you have the schema in place and tables generated, you can do it from MySQL side (Toad etc.,).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.