Hibernate注解配置1n,n1示例
package com.entity;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity
@Table(name="role")
public class RoleInfo {
private Integer roleId;
private String roleName;
private Set<UserInfo> users=new HashSet<UserInfo>();
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer getRoleId() {
return roleId;
}
public void setRoleId(Integer roleId) {
this.roleId = roleId;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
@OneToMany(mappedBy="role")
public Set<UserInfo> getUsers() {
return users;
}
public void setUsers(Set<UserInfo> users) {
this.users = users;
}
}
package com.entity;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedNativeQuery;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
@Entity
@Table(name="user")
public class UserInfo {
private Integer userId;
private String username;
private String password;
private String realName;
private Date birthday;
private RoleInfo role;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@ManyToOne
@JoinColumn(name="roleId")
public RoleInfo getRole() {
return role;
}
public void setRole(RoleInfo role) {
this.role = role;
}
@Override
public String toString() {
return "UserInfo [userId=" + userId + ", username=" + username
+ ", password=" + password + ", realName=" + realName
+ ", birthday=" + birthday + ", role=" + role + "]";
}
public UserInfo() {
}
public UserInfo(String username, String password) {
super();
this.username = username;
this.password = password;
}
}