Hibernate學習筆記_關係映射_其他

來源:互聯網
上載者:User

標籤: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;    }}

 

@Entitypublic class Student extends Person {        private int score;    public int getScore() {        return score;    }    public void setScore(int score) {        this.score = score;    }    }

 

 

@Entitypublic 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學習筆記_關係映射_其他

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.