`
chenguanwei2008
  • 浏览: 119126 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

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();
	}

}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics