JPA初步

注:例子来源于《Hibernate In Action》
必要的jar包:
在hibernate所需最小jar包基础上增加ejb3-persistence.jar和hibernate-annotation.jar

首先建立实体类Message.java,并使用Annotation来配置实体的映射关系:

package hello;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "MESSAGES")
public class Message {
	@Id @GeneratedValue
	@Column(name = "MESSAGE_ID")
	private Long id;
	
	@Column(name = "MESSAGE_TEXT")
	private String text;
	
	@ManyToOne(cascade = CascadeType.ALL)
	@JoinColumn(name = "NEXT_MESSAGE_ID")
	private Message nextMessage;
	
	public Message(){}
	
	public Message(String text) {
		this.text = text;
	}

	public Long getId() {
		return id;
	}

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

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public Message getNextMessage() {
		return nextMessage;
	}

	public void setNextMessage(Message nextMessage) {
		this.nextMessage = nextMessage;
	}
	
}

 然后在src目录下建立META-INF文件夹,并在在MEAT-INF中新建persistence.xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
             version="1.0">
    <persistence-unit name="helloworld">
        <!--
        <properties>
            <property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml"/>
        </properties>
    	 -->
    	 <provider>org.hibernate.ejb.HibernatePersistence</provider>
    	 
    	 <!-- Not needed,Hibernate supports auto-dection in JSE 
    	 	<class>hello.Message</class>
    	 -->
    	 
    	 <properties>
    	 	<!-- Auto detection -->
    	 	<property name="hibernate.archive.autodetection" value="class,hbm"/>
    	 	
    	 	<property name="hibernate.show_sql" value="true"/>
    	 	<property name="hibernate.format_sql" value="true"/>
    	 	<property name="hibernate.hbm2ddl.auto" value="update"/>
    	 	
    	 	<property name="hibernate.connection.driver_class" 
    	 			  value="com.mysql.jdbc.Driver"/>
    	 	<property name="hibernate.connection.url"
    	 			  value="jdbc:mysql://localhost:3306/hibernateEM"/>
    	 	<property name="hibernate.connection.username" value="root"/>
    	 	<property name="hibernate.connection.password" value="root"/>
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
    	
    		<!-- Use the C3P0 connection pool provider -->
			<property name="hibernate.c3p0.min_size" value="5"/>
			<property name="hibernate.c3p0.max_size" value="20"/>
			<property name="hibernate.c3p0.timeout" value="300"/>
			<property name="hibernate.c3p0.max_statements" value="50"/>
			<property name="hibernate.c3p0.idle_test_period" value="50"/>
    	 </properties>
    </persistence-unit>
</persistence>

 最后编写测试类:

package hello;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class HelloWorld {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		//start EntityManagerFactory
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("helloworld");
		//First unit of work
		EntityManager em = emf.createEntityManager();
		EntityTransaction tx = em.getTransaction();
		tx.begin();
		
		Message message = new Message("Hello World");
		em.persist(message);
		
		tx.commit();
		em.close();
		
		//Second unit of work
		EntityManager newEm = emf.createEntityManager();
		EntityTransaction newTx = newEm.getTransaction();
		newTx.begin();
		
		List messages = newEm.createQuery("select m from Message m order by m.text asc").getResultList();
		
		System.out.println(messages.size() + "messages(s) found");
		
		for(Object m : messages) {
			Message loadedMsg = (Message)m;
			System.out.println(loadedMsg.getText());
		}
		
		newTx.commit();
		newEm.close();
	
		//Shutting down the application
		emf.close();
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值