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

 


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值