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。
本文详细介绍了在EJB3.x中如何使用@UniqueConstraint注解确保数据库表中特定字段的唯一性。通过具体示例展示了如何正确设置columnNames属性来约束字段ROLE_NAME的唯一性。

283

被折叠的 条评论
为什么被折叠?



