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字段
}