hibernate 映射基本属性

hibernate 映射基本属性

新建一个po,News

public class News {
	private Long id;
	private String title;
	private String content;
	private String fullContent;
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getfullContent() {
		return fullContent;
	}
	public void setfullContent(String fullContent) {
		this.fullContent = fullContent;
	}
}

 对应的News.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.demo.po">
    <class name="News" table="news" lazy="true">
        <id name="id" column="news_id">
        	<!-- generate optimal identity strategy according to the configured dialect -->
            <generator class="native"/>
        </id>
      	<property name="title" type="string" not-null="true"/>
      	<property name="content" type="string" />
      	<!-- 通过formula指定该属性值没有对应的实际数据列
      		该属性值将由系统根据表达式来生成 -->
      	
       	<property name="fullContent"
      		formula="(select concat(nt.title,nt.content)
     			from news nt where nt.news_id = news_id)"/>
    </class>
</hibernate-mapping>

 测试类:NewsManager.java

 

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.hibernate.demo.po.News;
/**
 * 
 * ---test1---
 * 打开注释1的内容,保存news到数据库,然后把注释1重新注释掉,然后打开注释2,观察控制台输出的sql
 * 会发现,getfullContent()取到了你要的值,但是观察数据库news表,并没有fullContent字段
 * 
 * @author apprentice
 *
 */
public class NewsManager {
	public static void main(String[] args){
		//实例化Configuration,默认加载hibernate.cfg.xml
		Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
		//工厂模式,创建sessionFactory   
		SessionFactory sf = cfg.buildSessionFactory();
		//实例化session
		Session session = sf.getCurrentSession();
		//事务开始
		Transaction tx = session.beginTransaction();
/*      注释1		
		News news = new News();
		news.setTitle("学海无涯");
		news.setContent("泰山不辞杯土,大海不拒细流");
		session.save(news); 
*/
/*		注释2
		News news2 =(News)session.load(News.class,new Long(1));
		System.out.println(news2.getfullContent());
		tx.commit();
*/
	}
}

 test1对应的News.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.demo.po">
    <class name="News" table="news" lazy="true">
        <id name="id" column="news_id">
        	<!-- generate optimal identity strategy according to the configured dialect -->
            <generator class="native"/>
        </id>
      	<property name="title" type="string" not-null="true"/>
      	<property name="content" type="string" />
      	<!-- 通过formula指定该属性值没有对应的实际数据列
      		该属性值将由系统根据表达式来生成 -->
       	<property name="fullContent"
      		formula="(select concat(nt.title,nt.content)
     			from news nt where nt.news_id = news_id)"/>
    </class>
</hibernate-mapping>

 对应的news表结构

 

 

CREATE TABLE news(
	news_id BIGINT AUTO_INCREMENT PRIMARY KEY,
	title VARCHAR(255) NOT NULL,
	content VARCHAR(255)
)

 test2 

 

修改News.hbm.xm

 

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.demo.po">
    <class name="News" table="news" lazy="true">
        <id name="id" column="news_id">
        	<!-- generate optimal identity strategy according to the configured dialect -->
            <generator class="native"/>
        </id>
      	<property name="title" type="string" not-null="true"/>
      	<property name="content" type="string" />
     			
     	<!-- 数据库定义了一个insert 触发器,设置full_content -->	
		<property name="fullContent"
			column="full_content" type="string"
			generated="insert"/> 
    </class>
</hibernate-mapping>

 news表结构改为

 

 

CREATE TABLE news(
	news_id BIGINT AUTO_INCREMENT PRIMARY KEY,
	title VARCHAR(255) NOT NULL,
	content VARCHAR(255)
	full_content VARCHAR(255)
)

 对应的insert触发器

 

 

DELIMITER |
CREATE TRIGGER tri_full_content_gen BEFORE INSERT ON news
	FOR EACH ROW BEGIN
		SET new.full_content=CONCAT(new.title,new.content);
		END;
		|

 测试代码:

 

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.hibernate.demo.po.News;
/**
 * 
 * ---test2---
 * 
 * @author apprentice
 *
 */
public class NewsManager {
	public static void main(String[] args){
		//实例化Configuration,默认加载hibernate.cfg.xml
		Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
		//工厂模式,创建sessionFactory   
		SessionFactory sf = cfg.buildSessionFactory();
		//实例化session
		Session session = sf.getCurrentSession();
		//事务开始
		Transaction tx = session.beginTransaction();
	
		News news = new News();
		news.setTitle("学海无涯");
		news.setContent("泰山不辞杯土,大海不拒细流");
		session.save(news); 
		tx.commit();
	}
}

 结果截图:



 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值