jpa之eclipselink2.1之myeclipse10.0环境下配置实现

         myeclipse10.0自带的对jpa 2.0的支持,并且自带的支持eclipselink、hibernate等jpa产品的实现,现在利用myeclipse的工具将数据库中已存在的多个表建立关系(1:m,n:m),并且建立相应表的实体对象.

      oracle的版本为11g

     1.先在oracle中建立如下几张表:

--主要创建学生表、成绩表、课程表
--学生表与成绩表为1:m的关系 课程表与成绩表也是1:m的关系  而学生表与课程表是多对多的关系
--创建学生表 学生编号为主键
create table student(stu_id integer primary key,stu_name varchar2(8) not null,
stu_sex varchar2(2) not null,stu_age integer not null);
--创建课程表 课程编号为主键
create table cource(cou_id integer primary key,cou_name varchar2(15) not null,
cou_score number(2,1) not null);
--创建选课表 学生编号,课程编号为主键 同时也为外键
create table selcou(stu_id integer,cou_id integer,primary key(stu_id,cou_id),
constraint fk_stu_id foreign key(stu_id) references student(stu_id),
constraint fk_cou_id foreign key(cou_id) references cource(cou_id));
--创建成绩表 成绩编号为主键 学生编号,课程编号为外键
create table score(sco_id integer primary key,stu_id integer not null,
cou_id integer not null,grade number(3,1) not null,
constraint fk_score_stu_id foreign key(stu_id) references student(stu_id),
constraint fk_score_cou_id foreign key(cou_id) references cource(cou_id)
);


    2.新建一个java工程名为eclipselinkoracle,右键单击工程名,点击Myeclipse这一项,接着点击Add JPA Capabilities,会出现如下界面:

  

   这里选择jpa2.0 并且使用eclipselink2.1作为其产品实现,使用myeclipse自带的jpa2.0和eclipse2.1的jar即可(默认情况是如此) 点击下一步

这里添加一个oracle的jdbc的连接 点击Add Connection

   修改相应的配置参数 如下:

 

  点击test driver 如果参数没有问题的话 会弹出如下界面

  表示驱动连接配置成功 点击完成回到刚才的配置界面

如下

 点击完成

myeclipse会为你自动的添加相应的eclipselink与jpa2.0的jar包到工程中 并且为你创建persistence.xml文件  如下

现在新建实体对象从表中 在src上点击右键-->new-->other 在弹出的界面中 找到myeclipse-->jpa-->jpa entities from tables 如下

   点击下一步-->点击

 上图的note图标 然后就会让你输入连接数据库的密码 连上数据库后 会将你数据库中的表全部显示在界面上 如下:

选上4张表后 点击下一步

  因为之前在oracle中建立表结构的时候 我就已经指定了表之间的相互约束关系 所里这里如下图:

   学生表与课程表是多对多的关系 学生表与成绩表是一对多的关系 课程表与成绩表是一对多的关系 所以关系如上 如果 你没有在oracle中建立他们之间的约束关系 即没有指定外键 那么这里就不会生成向上面的三张关系 点击上图右边的加号 即可添加两个表之间的关系 这里修改一下他们之间的级联关系

  像学生表进行了增删改查的时候 级联修改成绩表 这里修改他们的级联关系 依据具体的情况而定

点击下一步 修改包名

点击下一步

 

  在这里你可以修改每一个字段映射的数据类型 还有种类 还有相应的get和set方法

   这里要注意了 myeclipse默认的 如果你的表结构的字段为nvarchar2和date的话 映射过来在实体对象中默认是object的

                        如果表结构的字段为varchar2的话 映射过来就是String了

点击完成后 你就会发现 myeclipse会默认的为你生成了 三个实体对象 如下

这里只是简单的做个测试 详细的步骤可以参看前面的jpa操作hibernete4.2.4之关联一对一、一对多、多对多

在Student.java中添加如下代码: 下面的addCource方法用于给学生表、课程表的关联表(选课表)中添加一行记录

      addScore方法用于给成绩表中传递学生的编号 做为外键给成绩表进行维护

public Student(long stuId, BigDecimal stuAge, String stuName,
			String stuSex) {
		super();
		this.stuId = stuId;
		this.stuAge = stuAge;
		this.stuName = stuName;
		this.stuSex = stuSex;
		
	}
	public void addCource(Cource cource)
	{
		this.cources.add(cource);
	}
	
	public void addScore(Score score)
	{
		score.setStudent(this);
		this.scores.add(score);
	}
private Set<Score> scores=new HashSet<Score>();;
private Set<Cource> cources=new HashSet<Cource>();




 

 

 


Cource.java 添加如下:下面的addScore用于向成绩表中传递课程表的课程id 作为外键给成绩表进行维护

public Cource(long couId, String couName, BigDecimal couScore) {
		super();
		this.couId = couId;
		this.couName = couName;
		this.couScore = couScore;
	}
	public void addScore(Score score)
	{
		score.setCource(this);
		this.scores.add(score);
	}
private Set<Score> scores=new HashSet<Score>();


 


Score.java中添加

public Score(long scoId, BigDecimal grade) {
		super();
		this.scoId = scoId;
		this.grade = grade;
	}


 

现在编写测试单元:

 

package com.junit;

import static org.junit.Assert.*;

import java.math.BigDecimal;

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

import org.junit.Test;

import com.undergrowth.Cource;
import com.undergrowth.Score;
import com.undergrowth.Student;

public class JunitTest {

	@Test
	public void test() {
		EntityManagerFactory factory=Persistence.createEntityManagerFactory("eclipselinkoracle");
		EntityManager manager=factory.createEntityManager();
		manager.getTransaction().begin();
		Score score=new Score(2001, new BigDecimal(90));
		Cource cource=new Cource(1001l, "高等数学", new BigDecimal(4.5f));
		//相当于1001这门课考了90分
		cource.addScore(score);
		Student student=new Student(0001l, new BigDecimal(20), "under", "男");
		//相当于学号为0001的同学选了一门1001的课程
		student.addCource(cource);
		//相当于0001的同学考了90分
		student.addScore(score);
		manager.persist(cource);
		manager.persist(student); //因为学生表与成绩表有级联保存关系 所以会保存成绩表
		manager.getTransaction().commit();
		manager.close();
		factory.close();
	}

}


 

oracle效果图:

   看到上面oracle的效果图 就明白了

其实上面测试代码 就干了一件是 就是学号为1的学生选了1001这门课 并且这门课考了90分

    其实在测试代码之前 添加的那一些代码 才是关键 

       主要记住这就话就好了:

                  数据都是从关系的被维护端(使用mappedBy标示)传递给关系的维护端,无论关系是1:1,1:m还是m:n

原文地址:https://www.cnblogs.com/liangxinzhi/p/4275592.html