Hibernate註解配置1n,n1樣本

來源:互聯網
上載者:User

標籤:

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;
 }
 
}

Hibernate註解配置1n,n1樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.