这几天学习eclipselink,发现真的和hibernate一样一样的,它唯一的优势可能就是它是J2EE推荐的规范。缺点可能就是它的文档比较少(大多是英文的),不像hibernate那样丰富而且还有很多人使用吧,毕竟众人拾柴火焰高,用大家都用的,肯定会获得更多前人的经验。但是上边给了任务,所以怎么也得简单了解一下Eclipselink,把我最近看官网的例子和看大神的博客,总结下来的东西写一下,留个纪念,万一eclipselink后边被大家用起来,甚至用得比hibernate好呢!你说对不??(*^__^*)嘻嘻……
下面咱们就来讲讲eclipseLink的那些事儿:
eclipseLink是一个开源的实体映射框架,可以用在Java环境中,包括j2se j2ee
eclipseLink完全实现了以下这些规范:
JPA(Java Persistence API):
JPA是Java API实现ORM的实体对象关系映射的,它的目的是在Java应用程序中管理关系数据
JPA包括 Java固有的查询语言(JPQL);Java的固有标准API;以及定义了Java api和xml的实体关系映射的格式。
eclipseLink是参考jpa来实现完成的。通常把eclipseLink来代替jpa的实现。
它的映射关系有:
@OneToOne |
@OneToMany |
@ManyToOne |
@ManyToMany |
它最常用的注解有:
@Id | Identifies the unique ID of the database entry(主键) |
@GeneratedValue | Together with ID defines that this value is generated automatically.(自增) |
@Transient | Field will not be saved in database(不会保存到数据库) |
它支持SQL和NOSQL
我感觉可讲的也就是它映射关系了,毕竟学习hibernate和Mybatis的时候都是从这个学起的,那么eclipselink咱们也从这个开始吧。(PS:感觉学完这个,也就没啥了)
先看看咱们只用上面3个注解,别的什么都不用,eclipselink生成数据库是什么样子的:
先建立一个project,引入下面三个包:
要注意版本,不然会报错的
然后建立一个映射文件:
这个映射文件是通用的,咱们做下面的所有例子都用这个配置文件:只是修改少数里面的内容:
先建立xml文件:
<?xml version="1.0" encoding="UTF-8"?> <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" version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"> <persistence-unit name="EmployeeService" transaction-type="RESOURCE_LOCAL"> <class>model.PersonInformation</class> <properties> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://192.168.81.129:3306/simpleDb" /> <property name="javax.persistence.jdbc.user" value="root" /> <property name="javax.persistence.jdbc.password" value="root" />
<!-- EclipseLink should create the database schema automatically --> <property name="eclipselink.ddl-generation" value="create-tables" /> <property name="eclipselink.ddl-generation.output-mode" value="database" /> </properties> </persistence-unit> </persistence> |
在一个<persistence-unit>的单元里定义了这个unit的名字以及详细的数据库连接驱动,数据库用户名,密码。前面<class>里面定义的是需要映射到数据库的具体实体类。也就是你需要设计的库长什么样,你就写model里面的什么字段,到时候通过这个<class>就可以创建我们需要的库。因为我们要连的数据库是mysql,这里的javax.persistence.jdbc.driver值被设为com.mysql.jdbc.Driver。而我们具体要连接的数据库名字在javax.persistence.jdbc.url对应的值里面定义了,为simpleDb。为了后面实际程序运行的时候能够读写这个库,我们需要事先在数据库里创建simpleDb。
然后是Model类:
package model;
import javax.persistence.Entity; import javax.persistence.Id;
@Entity public class PersonInformation {
@Id private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }
} |
很简单的一个entity bean对象定义。我们针对每个可以操作的对象定义了get, set方法。这里比较有意思的地方是我们用了两个注解,一个是@Entity,一个是@Id。这里的@Entity表示一个可以序列化映射的的对象。如果我们希望这个对象被映射到数据库中的某个表,则必须要加上这个注解。而@Id则表示对应表的主键。我们建一个表要求有对应的主键。这里指定id为主键。
然后是service(这里我们用测试类代替),运行Model在数据库创建表:
package main;
import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence;
import model.PersonInformation;
public class Main {
//和xml中的persistence-unit对应的Name一样才行 //如果我们需要访问多个库的话,在配置文件里也可以定义多个persistence-unit。 private static final String PERSISTENCE_UNIT_NAME = "EmployeeService"; private static EntityManagerFactory factory;
public static void main(String[] args) { factory = Persistence.createEntityManagerFactory( PERSISTENCE_UNIT_NAME); EntityManager em = factory.createEntityManager(); //保证事务 em.getTransaction().begin(); PersonInformation person = new PersonInformation(); person.setId(1); person.setAge(30); person.setName("fred"); em.persist(person); em.getTransaction().commit(); em.close(); } } |
运行Main方法,就会发现在mysql数据库中创建了一个表:
可以看到,它默认生成的表名和字段名都是大写的。可是如果我不想用大写的,我想用我自己设置的怎么办?
接着往下看:
如果你不想用它自动生成大写,那么就自己定制,定制的Model如下,注意加粗字体:
package model;
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table;
@Entity @Table(name="Person") public class Person { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="id") private int id;
@Column(name="name") private String name;
@Column(name="age") private int age;
public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } |
<?xml version="1.0" encoding="UTF-8"?> <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" version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"> <persistence-unit name="EmployeeService" transaction-type="RESOURCE_LOCAL"> <!-- <class>model.PersonInformation</class> --> <class>model.Person</class> <properties> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://192.168.81.129:3306/simpleDb" /> <property name="javax.persistence.jdbc.user" value="root" /> <property name="javax.persistence.jdbc.password" value="root" />
<!-- EclipseLink should create the database schema automatically --> <property name="eclipselink.ddl-generation" value="create-tables" /> <property name="eclipselink.ddl-generation.output-mode" value="database" /> </properties> </persistence-unit> </persistence> |
对应定制的main如下:
package main;
import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence;
import model.Person;
public class Main {
private static final String PERSISTENCE_UNIT_NAME = "EmployeeService"; private static EntityManagerFactory factory;
public static void main(String[] args) { factory = Persistence.createEntityManagerFactory( PERSISTENCE_UNIT_NAME); EntityManager em = factory.createEntityManager(); //保证事务 em.getTransaction().begin(); // PersonInformation person = new PersonInformation(); Person person =new Person(); person.setId(2); person.setAge(25); person.setName("Erica"); em.persist(person); em.getTransaction().commit(); em.close(); } } |
运行后生成Person表,就是自己定制的如下:
咋样,是不是很简单??好了,初步了解就到这里了,接下来就开始讲它的4中映射关系了,敬请期待!
PS:这个文章中的例子有一部分是参考的其他博主的博客,如果有侵权可以联系我,我可以删掉。