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
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;} }
注意,这里是一切从简的!
<?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程序就搞定了。
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.
- JPA hellworld
- mybatis hellworld
- chaincode-hellworld
- JNI HellWorld 简单尝试
- hellworld项目结构
- bash下hellworld
- Jenkins插件开发hellworld
- No.01 "Hellworld"
- layabox说明和输出hellworld
- 1.3java第一个程序HellWorld
- JPA
- JPA
- JPA
- JPA
- JPA
- JPA
- JPA
- jpa
- linux环境下编译 jrtplib 和 jthread
- ILayer
- PB导入Excel
- STL相关知识
- Java Bean 属性命名规范问题分析
- JPA hellworld
- objective c 语法系列之--Class level access
- 台达0.4KW伺服上电报AL013
- 网页设计中纯色背景 的例子
- ios-复制字符串到剪贴板
- 设计模式推导——前言
- eclipseCDT 如何启用断言
- ZedGraph
- 制作根文件系统