hibernate將每個類映射成表的繼承

來源:互聯網
上載者:User

前面的一種方式是3個類放在一個表中,這樣看上去不符合關聯式模式,但是效率快。所以就少第二種更加符合關聯式模式的方法。那就是將每一個類映射到每一個表。這樣使表結構更加清晰。但是執行效率會比較低。

 

我還是寫原來那個例子。

1.      基類

package com.fish.testdao;

 

public
class
Employee {

private Integer
id;

private String
name;

private Department
department;

public Integer getId() {

    return
id;

}

public
void
setId(Integer id) {

    this.id = id;

}

public String getName() {

    return
name;

}

public
void
setName(String name) {

    this.name = name;

}

public Department getDepartment() {

    return
department;

}

public
void
setDepartment(Department department) {

    this.department = department;

}

 

}

2.銷售類

package com.fish.testdao;

 

public
class
Saleextends Employee{

private
float
money;

 

public
float
getMoney() {

    return
money;

}

 

public
void
setMoney(float money) {

    this.money = money;

}

 

}

2.      技術工類

package com.fish.testdao;

 

public
class
Skill extends Employee{

private String
killer;

 

public String getKiller() {

    return
killer;

}

 

public
void
setKiller(String killer) {

    this.killer = killer;

}

 

}

然後寫一個xml

<?xml
version="1.0"
encoding="UTF-8"?>

<!DOCTYPE
hibernate-mapping PUBLIC

    "-//Hibernate/HibernateMapping DTD 3.0//EN"

    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 

<hibernate-mapping>

    <class
name="com.fish.testdao.Employee"
discriminator-value="0">

 

       <id
name="id"
column="id"type="integer">

           <generator
class="increment"></generator>

       </id>

 

       <property
name="name"></property>

 

       <many-to-one
name="department"></many-to-one>

 

       <joined-subclass
name="com.fish.testdao.Sale"table="sale">

           <key
column="emp_id"></key>

           <property
name="money"></property>

       </joined-subclass>

       <joined-subclass
name="com.fish.testdao.Skill"table="skill">

           <key
column="emp_id"></key>

           <property
name="killer"></property>

       </joined-subclass>

   

    </class>

</hibernate-mapping>

joined-subclass這個就是可以將該類映射到另外一個表中,然後利用key和原來employee表的id相關聯。這樣2張表就建立了聯絡。

 

下面寫一個測試類別

package com.fish.domain;

 

import java.util.List;

 

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

 

import com.fish.testdao.Department;

import com.fish.testdao.Skill;

 

public
class
Test6 {

    // 寫一個得到session的模板

    public
static
Session getMySession() {

       Configuration configuration = new Configuration();

       configuration.configure("hibernate.cfg.xml");

       SessionFactory factory = configuration.buildSessionFactory();

       Session session = factory.openSession();

       return session;

    }

 

    public
static void
add() {

       Session session = getMySession();

       Department department = new Department();

       department.setName("財務部");

       Skill skill = new Skill();

       skill.setDepartment(department);

       skill.setKiller("技工");

       skill.setName("妮妮");

       Transaction transaction = session.beginTransaction();

       transaction.begin();

 

       session.save(department);

       session.save(skill);

       transaction.commit();

       session.close();

    }

 

    public
static void
query() {

       String hql = "fromSkill";

       Session session = getMySession();

       Query query = session.createQuery(hql);

       List<Skill> list = query.list();

       for (Skill i : list) {

           System.out.println(i.getName()+"是"+i.getKiller());

       }

 

    }

 

    public
static void
main(String[] args) {

       // add();

       query();

    }

}

我們看看資料庫中的儲存結果。

聯繫我們

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