有两个实体:
案例
package com.igorgorbunov3333.core.entities.domain;
import javax.persistence.*;
/**
* Created by Игорь on 03.04.2016.
*/
@Entity
@Table(name = "cases")
@NamedQueries({
@NamedQuery(name = "findAllCases", query = "SELECT c FROM Case c"),
@NamedQuery(name = "findCasesWithJudge", query = "SELECT c FROM Case c " +
"WHERE c.firstInstance.judge.id = :id OR c.secondInstance.judge.id = :id OR c.thirdInstance.judge.id = :id")
})
public class Case {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "case_number")
private String caseNumber;
@Column(name = "client")
private String clientName;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "first_instance_id")
private FirstInstance firstInstance;
@OneToOne
@JoinColumn(name = "second_instance_id")
private SecondInstance secondInstance;
@OneToOne
@JoinColumn(name = "third_instance_id")
private ThirdInstance thirdInstance;
@ManyToOne
@JoinColumn(name = "category_id")
private Category category;
private float price;
}
第二个实体 FirstInstance:
package com.igorgorbunov3333.core.entities.domain;
import com.igorgorbunov3333.core.entities.enums.CaseStatus;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created by Игорь on 19.09.2016.
*/
@Entity
@Table(name = "first_instance")
public class FirstInstance {
@Id
private long id;
@Temporal(TemporalType.DATE)
private Date date;
@ManyToOne
@JoinColumn(name = "judge_id")
private Judge judge;
@ManyToOne
@JoinColumn(name = "lawyer_id")
private Lawyer lawyer;
@Enumerated(value = EnumType.ORDINAL)
private CaseStatus status;
@OneToMany(mappedBy = "firstInstance")
private List<CourtHearingFirst> hearings = new ArrayList<>();
}
如您所见,字段FirstInstance
具有引用类型,但与我的数据库中的数字列相关,它等于FirstInstances id。
我坚持Case
实体和FirstInstance
,同时坚持FirstInstance
它的id
字段等于,例如35,但字段实体FirstInstance
的{{1}}总是等于0,为什么?它也可能等于35 ......
这是我的Servlet代码:
Case
这是代码,它向您展示我如何持久化实体:
@WebServlet(name = "DoCreateCase", urlPatterns = "/doCreateCase")
public class DoCreateCase extends HttpServlet {
private final CategoryService categoryService = SpringAppContextManager.getAppContext()
.getBean("CategoryService", CategoryService.class);
private final JudgeService judgeService = SpringAppContextManager.getAppContext()
.getBean("JudgeService", JudgeService.class);
private final LawyerService lawyerService = SpringAppContextManager.getAppContext()
.getBean("LawyerService", LawyerService.class);
private final FirstInstanceService firstInstanceService = SpringAppContextManager.getAppContext()
.getBean("FirstInstanceService", FirstInstanceService.class);
private final CaseService caseService = SpringAppContextManager.getAppContext()
.getBean("CaseService", CaseService.class);
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Case lawsuit = new Case();
FirstInstance firstInstance = new FirstInstance();
String caseNumber = request.getParameter("case-number");
lawsuit.setCaseNumber(caseNumber);
String clientName = request.getParameter("client-name");
lawsuit.setClientName(clientName);
String dayOpen = request.getParameter("day-open"),
monthOpen = request.getParameter("month-open"),
yearOpen = request.getParameter("year-open");
Date date = DateUtil.toDate(yearOpen, monthOpen, dayOpen);
firstInstance.setDate(date);
long categoryId = Long.valueOf(request.getParameter("category"));
Category category = categoryService.findById(categoryId);
lawsuit.setCategory(category);
float price = Float.valueOf(request.getParameter("price"));
lawsuit.setPrice(price);
long judgeId = Long.valueOf(request.getParameter("judge"));
Judge judge = judgeService.findById(judgeId);
firstInstance.setJudge(judge);
long lawyerId = Long.valueOf(request.getParameter("lawyer"));
Lawyer lawyer = lawyerService.findById(lawyerId);
firstInstance.setLawyer(lawyer);
lawsuit.setFirstInstance(firstInstance);
caseService.create(lawsuit);
}
}
有什么问题?
答案 0 :(得分:0)
我同时坚持Case实体和
firstInstance
......
您的代码中没有任何地方{}持续存在firstInstance
。无论是否存在,您都不会将firstInstance
与lawsuit
相关联。
所以你需要打电话:
lawsuit.setFirstInstance(firstInstance);
然后调用entityManager.persist(lawsuit);
将:
(a)由于在关联上指定的Cascade选项,插入新的FirstInstance记录。
(b)插入Case实例,将FK设置为新持久化的FIrstInstance记录的ID。
答案 1 :(得分:-1)
我认为你的实体类应该实现Serializable,并且应该具有如下的serialversionUID:
public class FirstInstance implements Serializable{
private static final long serialVersionUID = 1L;
.....
}
public class Case implements Serializable{
private static final long serialVersionUID = 1L;
.....
}