`
keating
  • 浏览: 170044 次
  • 性别: Icon_minigender_1
  • 来自: weihai
社区版块
存档分类
最新评论

A-->List<B> 有两个..How JPA do ?

    博客分类:
  • Java
阅读更多
Compare to the previous one(I mean 'A-->B 有两个..How JPA do ?'), I only add a constructor to Class B:
import java.io.Serializable;
import javax.persistence.*;

/**
 * Entity implementation class for Entity: B
 * 
 */
@Entity
public class B implements Serializable {

	public B() {
		super();
	}
	
	public B(String name){
		super();
		this.name = name;		
	}

	@GeneratedValue(strategy = GenerationType.AUTO)
	@Id
	private int id;
	private String name;

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

}

This is A -> List<B> , so the Class A:
import static javax.persistence.CascadeType.PERSIST;
import static javax.persistence.CascadeType.REMOVE;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

/**
 * Entity implementation class for Entity: A
 * 
 */
@Entity
public class A implements Serializable {

	public A() {
		super();
	}

	@GeneratedValue(strategy = GenerationType.AUTO)
	@Id
	private int id;
	@OneToMany(cascade = { PERSIST, REMOVE })
	private List<B> b1;
	@OneToMany(cascade = { PERSIST, REMOVE })
	private List<B> b2;

	public List<B> getB1() {
		return b1;
	}

	public void setB1(List<B> b1) {
		this.b1 = b1;
	}

	public List<B> getB2() {
		return b2;
	}

	public void setB2(List<B> b2) {
		this.b2 = b2;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

}

Let's see the table A first:
There is only one field -- ID.
But, there is another table -- A_B, so we know, there is only one table exists between two relational entities.
A_B:
A_ID  B2_ID  B1_ID
Try it:
import java.util.ArrayList;
import java.util.List;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		List<B> b1 = new ArrayList<B>();
		b1.add(new B("b1"));
		b1.add(new B("b1."));
		
		List<B> b2 = new ArrayList<B>();
		b2.add(new B("b2"));
		b2.add(new B("b2."));

		A a = new A();
		a.setB1(b1);
		a.setB2(b2);
		OperateData.create(a);
		
		a = (A) OperateData.find(a);
		
		for(B b : a.getB1()){
			System.out.println("b1:" + b.getName());
		}
		for(B b : a.getB2()){
			System.out.println("b2:" + b.getName());
		}
		
	}

}

There is an error message -- '列“B2_ID”无法接受空值。'
What should we do ? Maybe I can add a property as Class B \'s state.
But, why don't use two different B ?
分享到:
评论

相关推荐

    spring4 mvc + jpa demo

    pom.xml 的引入的相关jar版本配置 &lt;properties&gt; &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt; ... &lt;hibernate-jpa.version&gt;1.0.1.Final&lt;/hibernate-jpa.version&gt; &lt;/properties&gt;

    spring-hibernate-dwr实例

    dao.jar&lt;br&gt;spring-hibernate3.jar&lt;br&gt;spring-ibatis.jar&lt;br&gt;spring-jdbc.jar&lt;br&gt;spring-jdo.jar&lt;br&gt;spring-jpa.jar&lt;br&gt;spring-toplink.jar&lt;br&gt;persistence.jar&lt;br&gt;spring-aop.jar&lt;br&gt;spring-agent.jar&lt;br&gt;spring-...

    spring+struts+hibernate+dwr+jstl做的实例

    logging.jar&lt;br&gt;log4j-1.2.14.jar&lt;br&gt;spring-dao.jar&lt;br&gt;spring-hibernate3.jar&lt;br&gt;spring-ibatis.jar&lt;br&gt;spring-jdbc.jar&lt;br&gt;spring-jdo.jar&lt;br&gt;spring-jpa.jar&lt;br&gt;spring-toplink.jar&lt;br&gt;persistence.jar&lt;br&gt;...

    hibernate-jpa-2.1-api-1.0.2.Final-API文档-中文版.zip

    赠送jar包:hibernate-jpa-2.1-api-1.0.2.Final.jar; 赠送原API文档:hibernate-jpa-2.1-api-1.0.2.Final-javadoc.jar; 赠送源代码:hibernate-jpa-2.1-api-1.0.2.Final-sources.jar; 赠送Maven依赖信息文件:...

    hibernate-jpa-2.1-api-1.0.2.Final-API文档-中英对照版.zip

    赠送jar包:hibernate-jpa-2.1-api-1.0.2.Final.jar; 赠送原API文档:hibernate-jpa-2.1-api-1.0.2.Final-javadoc.jar; 赠送源代码:hibernate-jpa-2.1-api-1.0.2.Final-sources.jar; 赠送Maven依赖信息文件:...

    spring springmvc jpa配置文件

    &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt; &lt;!-- 数据源 --&gt; &lt;properties&gt; &lt;property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mydb"/&gt; &lt;property ...

    SPRING MVC配置过程

    &lt;url-pattern&gt;*.do&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; 三、 Spring 配置 &lt;!-- Spring 配置 --&gt; &lt;listener&gt; &lt;listener-class&gt; org.springframework.web.context.ContextLoaderListener &lt;/listener-class&gt; &lt;/...

    springBoot+springjpa.docx

    &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt; &lt;/dependency&gt; &lt;!-- MySQL driver --&gt; &lt;dependency&gt; &lt;groupId&gt;mysql&lt;/groupId&gt; &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt; &lt;scope&gt;runtime&lt;/...

    java8+tomcat8+struts2.5+spring4.3+hibernate5.2框架搭建详细过程

    &lt;artifactId&gt;hibernate-jpa-2.1-api&lt;/artifactId&gt; &lt;version&gt;1.0.0.Final&lt;/version&gt; &lt;/dependency&gt; &lt;!-- 其他可能需要的依赖 --&gt; &lt;/dependencies&gt; ``` #### 三、Struts2 配置 **1. struts.xml**: - 定义包、...

    List<Map>转化为List工具类

    一般使用springjdbc、hibernate的sql查询,库获取到的数据都是List&lt;Map&lt;String, Object&gt;&gt;结果集,如果我们要转化为JavaBean,则需要做一系列的map.get(),然后obj.set()。 此工程中就是解决List&lt;Map&lt;String, Object&gt;...

    SSM三大框架的整合(技术学习).docx

    &lt;spring.version&gt;4.0.2.RELEASE&lt;/spring.version&gt; &lt;!-- mybatis 版本号 --&gt; &lt;mybatis.version&gt;3.2.6&lt;/mybatis.version&gt; &lt;/properties&gt; &lt;dependencies&gt; &lt;!-- Spring 核心库 --&gt; &lt;dependency&gt; &lt;groupId&gt;org....

    spring data jpa入门实例

    &lt;artifactId&gt;spring-data-jpa&lt;/artifactId&gt; &lt;version&gt;${spring.data.version}&lt;/version&gt; &lt;/dependency&gt; &lt;!-- Hibernate --&gt; &lt;dependency&gt; &lt;groupId&gt;org.hibernate&lt;/groupId&gt; &lt;artifactId&gt;hibernate-...

    springboot专栏 006 springboot整合mybatis-plus 增删改查 ModelAndView jsp

    &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt; &lt;/dependency&gt; &lt;!-- Mybatis-Plus --&gt; &lt;dependency&gt; &lt;groupId&gt;com.baomidou&lt;/groupId&gt; &lt;artifactId&gt;mybatis-plus-boot-starter&lt;/artifactId&gt; ...

    spring4-hibernate4-struts2整合

    &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;spring-core&lt;/artifactId&gt; &lt;version&gt;4.3.26.RELEASE&lt;/version&gt; &lt;/dependency&gt; &lt;!-- Spring Context --&gt; &lt;dependency&gt; &lt;groupId&gt;org.springframework&lt;/...

    hibernate-jpa-2.1-api-1.0.0.final-sources.jar

    hibernate-jpa-2.1-api-1.0.0.final-sources.jar 源码 hibernate-jpa-2.1-api-1.0.0.final-sources.jar 源码

    springMVC框架搭建及详解

    &lt;url-pattern&gt;*.do&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;!-- Spring配置 --&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;context-param&gt;...

    springboot 实例

    例如,我们有一个`Patrol`实体类,那么可以创建`PatrolMapper`接口和`PatrolMapper.xml`文件: ```java // PatrolMapper.java public interface PatrolMapper { Patrol selectById(Long id); List&lt;Patrol&gt; select...

    hibernate-jpa-2.0-api-1.0.1.Final.jar

    hibernate-jpa-2.0-api-1.0.1.Final.jar

    spring MVC配置详解

    Hibernate 3.6.8 需要引入的 jar 包有 hibernate3.jar、hibernate-jpa-2.0-api-1.0.1.Final.jar、antlr-2.7.6.jar、commons-collections-3.1、dom4j-1.6.1.jar、javassist-3.12.0.GA.jar、jta-1.1.jar、slf4j-api-...

    hibernate-jpa-2.1-api-1.0.0.final.jar.zip

    `hibernate-jpa-2.1-api-1.0.0.final.jar`是Hibernate对JPA 2.1规范的实现库,它使得开发者能够使用Hibernate的高效功能同时遵循JPA规范。 **1. Hibernate与JPA的关系** Hibernate最初作为一个独立的ORM框架,后来...

Global site tag (gtag.js) - Google Analytics