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 ?
分享到:
相关推荐
pom.xml 的引入的相关jar版本配置 <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> ... <hibernate-jpa.version>1.0.1.Final</hibernate-jpa.version> </properties>
dao.jar<br>spring-hibernate3.jar<br>spring-ibatis.jar<br>spring-jdbc.jar<br>spring-jdo.jar<br>spring-jpa.jar<br>spring-toplink.jar<br>persistence.jar<br>spring-aop.jar<br>spring-agent.jar<br>spring-...
logging.jar<br>log4j-1.2.14.jar<br>spring-dao.jar<br>spring-hibernate3.jar<br>spring-ibatis.jar<br>spring-jdbc.jar<br>spring-jdo.jar<br>spring-jpa.jar<br>spring-toplink.jar<br>persistence.jar<br>...
赠送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依赖信息文件:...
赠送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依赖信息文件:...
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <!-- 数据源 --> <properties> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mydb"/> <property ...
<url-pattern>*.do</url-pattern> </servlet-mapping> 三、 Spring 配置 <!-- Spring 配置 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </...
<artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- MySQL driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</...
<artifactId>hibernate-jpa-2.1-api</artifactId> <version>1.0.0.Final</version> </dependency> <!-- 其他可能需要的依赖 --> </dependencies> ``` #### 三、Struts2 配置 **1. struts.xml**: - 定义包、...
一般使用springjdbc、hibernate的sql查询,库获取到的数据都是List<Map<String, Object>>结果集,如果我们要转化为JavaBean,则需要做一系列的map.get(),然后obj.set()。 此工程中就是解决List<Map<String, Object>...
<spring.version>4.0.2.RELEASE</spring.version> <!-- mybatis 版本号 --> <mybatis.version>3.2.6</mybatis.version> </properties> <dependencies> <!-- Spring 核心库 --> <dependency> <groupId>org....
<artifactId>spring-data-jpa</artifactId> <version>${spring.data.version}</version> </dependency> <!-- Hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-...
<artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- Mybatis-Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> ...
<groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.26.RELEASE</version> </dependency> <!-- Spring Context --> <dependency> <groupId>org.springframework</...
<artifactId>spring-boot-starter-parent</artifactId> <version>2.3.2.RELEASE</version> </parent> <dependencies> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</...
hibernate-jpa-2.1-api-1.0.0.final-sources.jar 源码 hibernate-jpa-2.1-api-1.0.0.final-sources.jar 源码
<url-pattern>*.do</url-pattern> </servlet-mapping> <!-- Spring配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param>...
例如,我们有一个`Patrol`实体类,那么可以创建`PatrolMapper`接口和`PatrolMapper.xml`文件: ```java // PatrolMapper.java public interface PatrolMapper { Patrol selectById(Long id); List<Patrol> select...
hibernate-jpa-2.0-api-1.0.1.Final.jar
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-...