Hibernate annotations系列之繼承關係

Furniture 繼承 Woody ,Woody 繼承 Thingy  。

各個代碼如下:

 

 Thingy.java

 

//$Id: Thingy.java 11282 2007-03-14 22:05:59Z epbernard $
package org.hibernate.test.annotations.access;

import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

/**
 * @author Emmanuel Bernard
 */
@MappedSuperclass
public class Thingy {
@Transient
 private String god;


 public String getGod() {
  return god;
 }
 
 public void setGod(String god) {
  this.god = god;
 }
}

Woody.java

 

//$Id: Woody.java 11282 2007-03-14 22:05:59Z epbernard $
package org.hibernate.test.annotations.access;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;

/**
 * @author Emmanuel Bernard
 */
@MappedSuperclass
/* @AccessType("property") */
public class Woody extends Thingy {

 private String color;

 private String name;

 public boolean isAlive; // shouldn't be persistent

 
 public String getColor() {
  return color;
 }

 public void setColor(String color) {
  this.color = color;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
}

Furniture.java

//$Id: Furniture.java 11282 2007-03-14 22:05:59Z epbernard $
package org.hibernate.test.annotations.access;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.hibernate.annotations.AccessType;

/**
 * @author Emmanuel Bernard
 */
@Entity
@AccessType("field")
public class Furniture extends Woody {
 @Id
 @GeneratedValue
 private Integer id;
@Column(nullable=false)
 private String brand;
 public long weight;
 
/* @Transient*/
 public String getBrand() {
  return brand;
 }
 
 public void setBrand(String brand) {
  this.brand = brand;
 }
 
 public Integer getId() {
  return id;
 }

 public void setId(Integer id) {
  this.id = id;
 }


/* @AccessType("property")*/


 public long getWeight() {
  return weight + 1;
 }

 public void setWeight(long weight) {
  this.weight = weight + 1;
 }
}

 測試代碼:

 

package org.hibernate.test.annotations.access;

import org.hibernate.Session;

import annotations.HibernateUtil;

public class Main {

 public static void main(String[] args) throws Exception {
  long st = System.currentTimeMillis();

  Session s = HibernateUtil.getSession();

 s.beginTransaction();
 
  Furniture fur = new Furniture();
  fur.setGod("ddd");
  fur.setBrand("sss");
  fur.setColor( "Black" );
  fur.setName( "Beech" );
  fur.isAlive = true;
 
  
  fur.setWeight(new  Long(100));
  
  
  
  s.persist( fur );
  s.clear();
  /*tx = s.beginTransaction();
  fur = (Furniture) s.get( Furniture.class, fur.getId() );
  
  
  s.delete( fur );*/
  
  
  
  
  
  
  
  
 
  
  /* Customer c=(Customer)  s.load(Customer.class, new Long(1));
  
  
 s.delete(c);
 
   */
  
 /* Client c=new Client();
  
  c.setName("new");
  
  Address  add=new  Address();
  
  add.setCity("pair");
  
  
  c.setAddress(add);
  
  s.save(c);*/
  
  
  
/* Customer c = new Customer();
  c.setName( "Hibernatus" );
  
  Passport p = new Passport();
  p.setNumber( "123456789" );
  s.persist( c ); //we need the id to assigned it to passport
  
  c.setPassport( p );
  p.setOwner( c );
  p.setId( c.getId() );
 */
  
  
  
  
  
 /* Client c=(Client) s.load(Client.class, 1);
  
  
  c.getAddress().getCity();*/
  
 /* Client c = new Client();
  c.setName( "sss" );
  Address a = new Address();
  a.setId(100);
  
  a.setCity( "ff" );
  c.setAddress( a );
  
  s.save(c);*/
  
  
/* Body b=(Body)s.load(Body.class, new Integer(1000));
  
 b.getHeart().getClass();*/
  
 
/* Body  b=new Body();
  b.setId(new  Integer(1000));
  
  Heart  h=new Heart();
   h.setId(new Integer(1000));
  

  b.setHeart(h);
  
  
  
  s.save(b);
  
  s.save(h);*/
  
  
  s.flush();
  
  s.getTransaction().commit();

  s.close();
  long end = System.currentTimeMillis();
  System.out.println("用時: ");
  System.out.print(end - st);

 }

 

各個annotation解釋:

 

@MappedSuperclass  指定進行實體表的列的映射,在上面的例子中如果 Thingy 沒有@MappedSuperclass,則不會在數據庫表中產生 god字段

 


}

發佈了35 篇原創文章 · 獲贊 0 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章