JPA hellworld

来源:互联网 发布:求剑若此不亦惑乎 编辑:程序博客网 时间:2024/05/01 00:40

最近项目用到了jpa(什么是jpa?)

网上的中文教程也有,好像基本上是用的hibernate,我们现在公司要求用glassfish作为服务器,玻璃鱼已经有自己的jpa实现,就不用缘木求鱼,何况这里就是一个玻璃鱼呢。glassfish v2用的是toplink实现,toplink是oracle公司的,现在我用的是glassfish 3.1.1 用的是什么jpa实现的呢?安装完glassfish之后,在${glashfish_home}\glassfish\modules 中有org.eclipse.persistence.core.jar 文件,用winrar打开查看里面有一个 about.html文件,about.html说这个是 

EclipseLink 2.3.0

好吧,为啥不用toplik而用eclipselink了呢??是不是oracle把它的toplink共享给了eclipselink了呢?两者关系 有兴趣的可以探讨一下~
我用的是glassfish,其jpa实现是eclipselink2.3.0.好,下面开始第一个jpa程序。只是的一个程序,没有任何的技术含量,要想看有技术含量的到这个网站看:http://java.sun.com/developer/technicalArticles/J2SE/Desktop/persistenceapi/
用eclipse新建一个项目,名字叫hello_jpa引入依赖的jar包(可以把modules里面所有的jar都倒进去哈)我用的是

这几个包。(我是单独下载的eclipselink---主要是有文档!)
注意jpa的目录结构:在src下面要有一个META-INF文件夹,里面要有一个persistence.xml 文件,这些都是不能改变的!!!
好了下面就是编码了。
创建一个Entity类
package biz.inspeed.jpa;import java.io.Serializable;import javax.persistence.*;/** * Entity implementation class for Entity: CatEo * */@Entity@Table(name = "tb_cat")public class CatEo implements Serializable {private static final long serialVersionUID = 1L;public CatEo() {super();}@Id@Column(name = "cat_id")private Integer id;@Column(name = "cat_name")private String name;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}   }

注意,这里是一切从简的!
设置persistence.xml里的内容:

<?xml version="1.0" encoding="UTF-8"?><persistence version="2.0" 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_2_0.xsd"><persistence-unit name="hello_jpa" transaction-type="RESOURCE_LOCAL"><class>biz.inspeed.jpa.CatEo</class><class>biz.inspeed.jpa.Property</class><properties><property name="javax.persistence.jdbc.url" value="jdbc:mysql://192.168.0.52:3306/world"/><property name="javax.persistence.jdbc.user" value="hello"/><property name="javax.persistence.jdbc.password" value="hello"/><property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/></properties></persistence-unit><persistence-unit name="hello_jpa1" transaction-type="RESOURCE_LOCAL"><provider>org.eclipse.persistence.jpa.PersistenceProvider</provider><class>biz.inspeed.jpa.CatEo</class><class>biz.inspeed.jpa.Property</class><properties><property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/manage"/><property name="javax.persistence.jdbc.user" value="root"/><property name="javax.persistence.jdbc.password" value="mysql"/><property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/><!--  <property name="eclipselink.ddl-generation" value="create-tables"/>--></properties></persistence-unit></persistence>

那个mysql的driver可以自己下载。
然后就可以写个测试类来。

package biz.inspeed.jpa.test;import java.lang.reflect.Field;import java.util.Random;import javax.persistence.EntityManager;import javax.persistence.EntityManagerFactory;import javax.persistence.Persistence;import org.eclipse.persistence.config.PersistenceUnitProperties;import biz.inspeed.jpa.CatEo;import biz.inspeed.jpa.Property;public class Test {public static void main(String[] args) throws Exception, Throwable {EntityManagerFactory factory = Persistence.createEntityManagerFactory("hello_jpa1"); System.out.println(factory.getClass().getCanonicalName());EntityManager manager = factory.createEntityManager();CatEo cat = new CatEo();int id = new Random().nextInt();cat.setId(id);StringBuffer sb = new StringBuffer(50);sb.append("cat").append(id);cat.setName(sb.toString());manager.getTransaction().begin();manager.persist(cat);System.out.println(manager.contains(cat));CatEo find = manager.find(CatEo.class, id);System.out.println(find.getName());manager.getTransaction().commit();///---------------------------PersistenceUnitProperties properties = new PersistenceUnitProperties();Field[] fields = PersistenceUnitProperties.class.getDeclaredFields();manager.getTransaction().begin();for(Field f : fields){Property p = new Property();f.setAccessible(true);p.setName(f.getName());p.setValue(String.valueOf(f.get(properties)));manager.persist(p);}manager.getTransaction().commit();}}

好了,现在基本上第一个jpa程序就搞定了。
恩在persistence规范上说是有两种方法来用来实现entity 与数据表中的列实现对应,在sun官网教程上有这么句,感觉挺能说明问题,现在抄录下来分享一下:

Fields and Properties

An entity's state is defined by the value of its fields or properties. You get to decide whether the Java Persistence API uses your variable fields or your property getters and setters when retrieving and saving entities. If you annotate the instance variables themselves, the persistence provider will directly access the instance variables. If you annotate your JavaBean-style getter and setter methods, the persistence provider will use those accessors for loading and storing persistent state. You should choose one style or the other; mixing the two is illegal in the current specification. The current persistence specification shows examples that have annotated property accessors, so this article will follow that convention.

 


原创粉丝点击