• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Add constraints to table with Hibernate.

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If my assumption is correct, Hibernate is more on transactions and Data Manipulation and not in the Date Definition. I would like to know

How can i add constraints to table with Hibernate?
The two ways that strikes my mind is
a. In the setter method of the pojo class, add the constraint. But i need to know if this will work well in all the case.
b. using native sql, alter the table and add the constraint.

Please let me know which is correct way to add the constraint in the table.

thanks
Arjun.
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hibernate will create the constraints no problem. That's what it does.

Here are two classes, where a Team has many players, taken from my tutorial on mapping one to many relationships with Hibernate and JPA annotations.:





Now the other class:


Feeding this to Hibernate's SchemaExport class, you get the following SQL/DDL, which included the appropriate foreign key constraints.



Mapping Relationships and Creating Foreign Key Constraints with Hibernate and JPA

Hibernate is a full featured data framework. It's got all of the bells and whistles you need, and more.

-Cameron McKenzie






 
Arjun Abhishek
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response.
But I was looking for something like this, say a constraint like Check. say to check age is between 1 and 100.

hibernate equivalent for the these statement.

Alter table Person
Add constraint ageCheck (age>= 1 and age <=100)

thanks
Karthick.
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are talking of CRUD, Hibernate's good as it works on concrete database tables mapped in the hbms as meta-data for Hibernate.
Modifying the DB would again involve modifying the hbm file.
 
Cameron Wallace McKenzie
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a variety of annotations that impact how SQL is generated. Just some of the annotations for a Column are as follows:



Now those are just JPA specific. Hibernate has extensions that go beyond the JPA specification as well. You'd have to research a bit deeper to find out how granular the types of constraints on columns and tables can be.

Kindest regards,

-Cameron McKenzie

 
Arjun Abhishek
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you mean that I can add/represent constraint like check in the mappings of the entity.

thanks
Arjun.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic