• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Add constraints to table with Hibernate.

 
Arjun Abhishek
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • 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.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Reehan Lalkhanwar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Did you mean that I can add/represent constraint like check in the mappings of the entity.

thanks
Arjun.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic