简体   繁体   中英

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

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.

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.

在新的“JPA项目屏幕”上找到最新的JPA

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM