EJB3.x:关于@UniqueConstraint标记中的columnNames属性
例如以下代码:
- package net.model.entity;
-
- import java.io.Serializable;
-
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.Table;
- import javax.persistence.UniqueConstraint;
-
- @Entity
- @Table(
- name="ROLE" ,
- uniqueConstraints={@UniqueConstraint(columnNames={"ROLE_NAME"})}
- )
- public class Role implements Serializable
- {
- private Integer id ;
-
- private String roleName;
-
- private int roleValue ;
-
-
-
- @Id
- @GeneratedValue(strategy=GenerationType.AUTO)
- @Column(name="ROLE_ID")
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
-
-
- @Column(name="ROLE_NAME" , nullable=false ,length=50)
- public String getRoleName() {
- return roleName;
- }
-
- public void setRoleName(String roleName) {
- this.roleName = roleName;
- }
-
- @Column(name="ROLE_VALUE" , nullable=false)
- public int getRoleValue() {
- return roleValue;
- }
-
- public void setRoleValue(int roleValue) {
- this.roleValue = roleValue;
- }
-
- }
注意uniqueConstraints={@UniqueConstraint(columnNames={"ROLE_NAME"})} 中的columnNames属性值为数据库表中的字段名,而不是Role类中的类成员名roleName。