2015年11月13日 星期五

繼承映射三(table per class hierarchy) (Hibernate3.x 十八)

※table per class hierarchy

※準備資料

DROP TABLE CHESS;
CREATE TABLE CHESS (
    ID NUMBER(5),
    NAME VARCHAR(10),
    PRICE NUMBER(5),
    PRODUCT_DATE DATE,
    SCORE NUMBER(3),
    SCHOOL VARCHAR(20),
    SALARY NUMBER(6),
    COMPANY VARCHAR(20),
    JUDGE VARCHAR(1),
    CONSTRAINT PK_CHESS PRIMARY KEY(ID)
);

※將所有的欄位全部放在一張表,然後多新增一個判斷的欄位,以上例就是JUDGE
如果是S,那SALARY和COMPANY就不使用;
如果是M,那SCORE和SCHOOL就不使用


※XML設定

Chess.java

public abstract class Chess {
    private Integer id;
    private String name;
    private Integer price;
    private Date productDate;
    //setter/getter...
}

Maker.java

public class Maker extends Chess {
    private Integer salary;
    private String company;
    //setter/getter...
}

Student.java

public class Student extends Chess {
    private Integer score;
    private String school;
    //setter/getter...
}

Chess.hbm.xml

<class name="vo.Chess" abstract="true">
    <id name="id" type="java.lang.Integer">
        <column name="ID" />
        <generator class="assigned" />
    </id>
    
    <discriminator column="JUDGE" type="string" />
    
    <property name="name" type="java.lang.String">
        <column name="NAME" />
    </property>
    
    <property name="price" type="java.lang.Integer">
        <column name="PRICE" />
    </property>
    
    <property name="productDate" type="java.util.Date">
        <column name="PRODUCT_DATE" />
    </property>
    
    <subclass name="vo.Student" discriminator-value="S">
        <property name="score" type="java.lang.Integer">
            <column name="SCORE" />
        </property>
        <property name="school" type="java.lang.String">
            <column name="SCHOOL" />
        </property>
    </subclass>
    
    <subclass name="vo.Maker" discriminator-value="M">
        <property name="salary" type="java.lang.Integer">
            <column name="SALARY" />
        </property>
        <property name="company" type="java.lang.String">
            <column name="COMPANY" />
        </property>
    </subclass>
</class>

※注意discriminator和subclass的discriminiator-value,順序也要注意

測試類

Session s = HibernateUtil2.getSession();
Transaction tx = s.beginTransaction();
try {
    System.out.println("-----測試學生-----");
    Student stu = new Student();
    stu.setId(1);
    stu.setName("象棋");
    stu.setPrice(50);
    stu.setProductDate(new Date(100, 0, 1));
    stu.setScore(78);
    stu.setSchool("家裡蹲大學");
    s.save(stu);
    
    System.out.println("-----測試員工-----");
    Maker mak = new Maker();
    mak.setId(2);
    mak.setName("五子棋");
    mak.setPrice(30);
    mak.setProductDate(new Date(102, 11, 12));
    mak.setSalary(43000);
    mak.setCompany("少林寺管委會");
    s.save(mak);
    tx.commit();
    
    Student stuRtn = (Student) s.get(Student.class, 1);
    System.out.println("學校名:" + stuRtn.getSchool());
    
    Maker makRtn = (Maker) s.get(Maker.class, 2);
    System.out.println("公司名:" + makRtn.getCompany());
} catch (Exception e) {
    System.err.println("例外錯誤!");
    e.printStackTrace();
    tx.rollback();
    tx.rollback();
} finally {
    if (s.isOpen()) {
        s.close();
    }
}

※控制台的訊息,會自動將JUDGE塞值



※Annotation設定

hibernate.cfg.xml

<mapping class="vo.Chess" />
<mapping class="vo.Student" />
<mapping class="vo.Maker" />

Chess.java

@DiscriminatorColumn(name="JUDGE", discriminatorType = DiscriminatorType.STRING)
@Entity
@Table(name = "CHESS")
public abstract class Chess {
    private Integer id;
    private String name;
    private Integer price;
    private Date productDate;
    
    @Id
    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 Integer getPrice() {
        return price;
    }
    
    public void setPrice(Integer price) {
        this.price = price;
    }
    
    @Column(name = "PRODUCT_DATE")
    public Date getProductDate() {
        return productDate;
    }
    
    public void setProductDate(Date productDate) {
        this.productDate = productDate;
    }
}

Maker.java

@DiscriminatorValue("M")
@Entity
public class Maker extends Chess {
    //...
}

Student.java

@DiscriminatorValue("S")
@Entity
public class Student extends Chess {
    //...
}


沒有留言:

張貼留言