码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate学习笔记_关系映射_其他

时间:2014-12-05 12:31:41      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   for   strong   

集合映射(不太重要)

1         Set

2        List (与Set差不多 多个@OrderBy)

    a) @OrderBy     排序

private List<User> users = new ArrayList<User>();
    
    @OneToMany(mappedBy="group",
            cascade={CascadeType.ALL}
            )
    @OrderBy("name ASC")
    public List<User> getUsers() {
        return users;
    }
    public void setUsers(List<User> users) {
        this.users = users;
    }

 

3        Map

    a) @Mapkey

private Map<Integer, User> users = new HashMap<Integer, User>();
    
    @OneToMany(mappedBy="group", cascade=CascadeType.ALL)
    @MapKey(name="id")
    public Map<Integer, User> getUsers() {
        return users;
    }
    public void setUsers(Map<Integer, User> users) {
        this.users = users;
    }

 

@Test
    public void testLoadGroup() {
        
        
        Session s = sessionFactory.getCurrentSession();
        s.beginTransaction();
        Group g = (Group)s.load(Group.class, 1);
        for(Map.Entry<Integer, User> entry : g.getUsers().entrySet()) {
            System.out.println(entry.getValue().getName());
        }
        s.getTransaction().commit();
        
    }

 

 

继承映射(不太重要)

一      三种方式

a)    一张总表SINGLE_TABLE(数据库中生成一张表)

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="discriminator", discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue("person")
public class Person {
    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;
    }

}

 

 

@Entity
@DiscriminatorValue("student")
public class Student extends Person {
    
    private int score;

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
    
}

 

 

@Entity
@DiscriminatorValue("teacher")
public class Teacher extends Person {
    private String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    
}

 

 

  @Test
    public void testSave() {
        Student s = new Student();
        s.setName("s1");
        s.setScore(80);
        Teacher t = new Teacher();
        t.setName("t1");
        t.setTitle("中级");
        
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(s);
        session.save(t);
        session.getTransaction().commit();
        session.close();
    }
    @Test
    public void testLoad() {
        
        testSave();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        Student s = (Student)session.load(Student.class, 1);
        System.out.println(s.getScore());
        Person p = (Person)session.load(Person.class, 2);
        System.out.println(p.getName());
        session.getTransaction().commit();
        session.close();
        
    }

 

 

 

b)    每个类分别一张表TABLE_PER_CLASS

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
@TableGenerator(
        name="t_gen",
        table="t_gen_table",
        pkColumnName="t_pk",
        valueColumnName="t_value",
        pkColumnValue="person_pk",
        initialValue=1,
        allocationSize=1
        )
public class Person {
    private int id;
    private String name;
    
    @Id
    @GeneratedValue(generator="t_gen", strategy=GenerationType.TABLE)
    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;
    }

}

 

@Entity
public class Student extends Person {
    
    private int score;

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
    
}

 

 

@Entity
public class Teacher extends Person {
    private String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    
}

 

 

 

c)    每个子类一张表jOINED

 

父类上加注解@Inheritance(strategy=InheritanceType.JOINED)

@Entity
@Inheritance(strategy=InheritanceType.JOINED)

public class Person {
    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学习笔记_关系映射_其他

标签:style   blog   io   ar   color   os   sp   for   strong   

原文地址:http://www.cnblogs.com/enjoy-life-clh/p/4146229.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!