欢迎,
我有 2 节课:对话和提问。一次对话有很多问题。
对话.java:
package com.jcg.jpa.mappedBy;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "CONVERSATION_TABLE")
public class Conversation implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "CONV_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int conversationId;
@Column(name = "CONV_NAME")
private String name;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy="conversation")
private Collection<Question> questions = new ArrayList<Question>();
public Conversation() { }
public int getConversationId() {
return conversationId;
}
public void setConversationId(int conversationId) {
this.conversationId = conversationId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Collection<Question> getQuestions() {
return questions;
}
public void setQuestions(Collection<Question> questions) {
this.questions = questions;
}
@Override
public String toString() {
return "Employee [conversationId=" + conversationId + ", name=" + name + "]";
}
}
问题.java:
package com.jcg.jpa.mappedBy;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "QUESTION_TABLE")
public class Question implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToOne
@JoinColumn(name = "CONVERSATION_CONV_ID", nullable = false)
private Conversation conversation;
@Column(name = "QUESTION_TEXT")
private String questionText;
@Column(name = "ANSWER_TEXT")
private String answerText;
public Question() { }
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getQuestionText() {
return questionText;
}
public void setQuestionText(String questionText) {
this.questionText = questionText;
}
public String getAnswerText() {
return answerText;
}
public void setAnswerText(String answerText) {
this.answerText = answerText;
}
public Conversation getConversation() {
return conversation;
}
public void setConversation(Conversation conversation) {
this.conversation = conversation;
}
@Override
public String toString() {
return "Question [id=" + id + ", questionText=" + questionText
+ ", answerText=" + answerText +"]";
}
}
持久性.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="JPAMappedbyExample" transaction-type="RESOURCE_LOCAL">
<class>com.jcg.jpa.mappedBy.Conversation</class>
<class>com.jcg.jpa.mappedBy.Question</class>
<!-- Configuring The Database Connection Details -->
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpatest" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="qwerty" />
</properties>
</persistence-unit>
现在在 Main.java 我试图用两个问题创建对话:
package com.jcg.jpa.mappedBy;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class Main {
private static final EntityManagerFactory emFactoryObj;
private static final String PERSISTENCE_UNIT_NAME = "JPAMappedbyExample";
static {
emFactoryObj = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
}
// This Method Is Used To Retrieve The 'EntityManager' Object
public static EntityManager getEntityManager() {
return emFactoryObj.createEntityManager();
}
private static void insertRecords() {
EntityManager entityMgrObj = getEntityManager();
if (null != entityMgrObj) {
entityMgrObj.getTransaction().begin();
Conversation conv = new Conversation();
conv.setName("Discussion about something");
Question question1 = new Question();
question1.setQuestionText("2 plus 2");
question1.setAnswerText("four");
question1.setConversation(conv);
Question question2 = new Question();
question2.setQuestionText("what is Your name");
question2.setAnswerText("Adam");
question2.setConversation(conv);
List<Question> questions = new ArrayList<Question>();
questions.add(question1);
questions.add(question2);
conv.setQuestions(questions);
entityMgrObj.persist(conv);
entityMgrObj.getTransaction().commit();
entityMgrObj.clear();
System.out.println("Record Successfully Inserted In The Database");
}
}
public static void main(String[] args) {
insertRecords();
}
}
在 insertRecords() 我正在创建 conv 和两个问题。每个问题都设置了对话:
question1.setConversation(conv);
question2.setConversation(conv);
接下来,创建一个包含这 2 个问题的问题列表并将其设置为 conv questions list:
conv.setQuestions(questions);
它工作正常,因为数据被插入到两个表中,并且外键 CONVERSATION_CONV_ID 被填充:
但是,当我删除问题中的设置对话时,行:
question1.setConversation(conv);
question2.setConversation(conv);
外键设置为 NULL。为什么?我们已经在对话问题列表中添加了两个问题:
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy="conversation")
private Collection<Question> questions = new ArrayList<Question>();
,所以hibernate应该知道这两个问题的会话外键是什么(因为它们位于指定的会话问题列表中)。那么是否可以避免为每个问题设置 Conversation 并且只将问题添加到 Conversation 的问题列表中?我应该如何配置实体来做到这一点?或者也许这是不可能的,我们总是需要设置它的两个方向?