hibernate注解 多对一 外键关联

实体类 User 类 

package cn.hjc.model; 
import javax.persistence.CascadeType; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.ManyToOne; 
@Entity 
public class User { 
    private int id; 
    private String name; 
    private Group group; 
     
    @ManyToOne(cascade = {CascadeType.ALL}) 
    public Group getGroup() { 
        return group; 
    } 
    public void setGroup(Group group) { 
        this.group = group; 
    } 
    @Id 
    @GeneratedValue 
    public int getId() { 
        return id; 
    } 
    public void setId(int id) { 
        this.id = id; 
    } 
    public String getName() { 
        return name; 
    } 
    public void setName(String name) { 
        this.name = name; 
    } 
     
} 

 Group类 

package cn.hjc.model; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.Table; 
 
@Entity 
@Table(name="t_group") 
public class Group { 
    private int id; 
    private String name; 
     
    @Id 
    @GeneratedValue 
    public int getId() { 
        return id; 
    } 
     
    public void setId(int id) { 
        this.id = id; 
    } 
    public String getName() { 
        return name; 
    } 
    public void setName(String name) { 
        this.name = name; 
    }     
} 
  在hibernate.cfg.xml文件中加  

 
<mapping class="cn.hjc.model.User"/> 
<mapping class="cn.hjc.model.Group"/> 

你可能感兴趣的:(多对一,一 外键关联,hibernate注解,hibernate注解 多对一)