how to generate unique constraint in entity class using jpa project eclipse

Viewed 1161

I am planning to integrate ORM in my project, so i used eclipse's JPA project to generate entity classes out of old databases. In total 168 entity classes where generated and it's fine. But in some constraints like nullable, unique are not automatically generating.

For example I need something like this :-

@Column(name="USER_NAME",unique = true)
private String userName;

but after auto generating entities there is no unique constraint in code. How can I achieve this simply?

any suggestions will be useful.

4 Answers

Check your JPA version in your eclipse, if you found lowest versions update it to latest one. Latest JPA creates all the constraints which we defined in database table.
hope this helps you.

Find Latest JPA on New "JPA project screen"

@Unique represents a unique index that prevents duplicate values in the indexed field. A PersistenceException is thrown on commit (or flush) if different entities have the same value in a unique field (similar to how primary keys behave). In your exemple :

@Unique
@Column(name="USER_NAME")
private String userName;

The unique constraint will only be created if you create the table. If the table already exists only new columns will be added during DDL updates. So you have to either drop the table manually or set the value for javax.persistence.schema-generation.database.action to drop-and-create.

YOu have to use the Hibernate Tools from Jboss, which enable you to do a better reverse engineering.

http://hibernate.org/tools/

There are a bunch of tutorials outside which are explaining how to use the hibernate tools.

Related