File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Add constraints to table with Hibernate. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Add constraints to table with Hibernate." Watch "Add constraints to table with Hibernate." New topic
Author

Add constraints to table with Hibernate.

Arjun Abhishek
Ranch Hand

Joined: Jul 08, 2008
Posts: 57
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.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

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

Joined: Jul 08, 2008
Posts: 57
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.
Reehan Lalkhanwar
Ranch Hand

Joined: Jun 16, 2007
Posts: 106
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.


Thank you,
Reehan
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

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

Joined: Jul 08, 2008
Posts: 57
Did you mean that I can add/represent constraint like check in the mappings of the entity.

thanks
Arjun.
 
 
subject: Add constraints to table with Hibernate.
 
Similar Threads
Importing data into a database with a complex schema using Java
Not-unique unique column
Hibernate Unique Key attribute
Hibernate - impact of missing foreign key in database
Why? uniqueConstraints is not working!