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