当我运行我的应用程序时,抛出如下异常:
严重:加载应用程序时出现异常 严重:上下文/wren6-fp 取消部署失败 严重: Exception while loading the app : jakarta.ejb.CreateException: Initialization failed for Singleton DBIinitService jakarta.ejb.CreateException:单例 DBInitService 初始化失败
Caused by: jakarta.ejb.EJBTransactionRolledbackException: Exception thrown from bean: jakarta.validation.ConstraintViolationException: One or more Bean Validation constraints were violated while executing Automatic Bean Validation on callback event: prePersist for class: edu.iit.sat.itmd4515.wren6.domain.Order. Please refer to the embedded constraint violations for details.
at com.sun.ejb.containers.BaseContainer.mapLocal3xException(BaseContainer.java:2386)
但是谁能告诉我如何找到具体的违规行为? 这是我的订单实体代码:
package edu.iit.sat.itmd4515.wren6.domain;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.Table;
import jakarta.validation.constraints.PastOrPresent;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
*
* @author Administrator
*/
@Entity
@Table(name = "BOOK_ORDERS")
@NamedQuery(name="Order.findAll", query="select o from Order o")
public class Order {
@Column(name = "ORDER_ID")
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "ORDER_TYPE")
@Enumerated(EnumType.STRING)
private OrderType type;
@PastOrPresent
@Column(name = "ORDER_DATE")
private LocalDate date;
@PastOrPresent
@Column(name = "ORDER_TIME")
private LocalTime time;
@ManyToMany
@JoinTable(name = "ORDER_SUBJECTS",
joinColumns = @JoinColumn(name = "ORDER_ID"),
inverseJoinColumns = @JoinColumn(name = "SUBJECT_ID"))
private List<Subject> subjects = new ArrayList<>();
@ManyToOne
@JoinColumn(name = "HOLDER_ID")
private Holder holder;
/**
* Get the value of subjects
*
* @return the value of subjects
*/
public List<Subject> getSubjects() {
return subjects;
}
/**
* Set the value of subjects
*
* @param subjects new value of subjects
*/
public void setSubjects(List<Subject> subjects) {
this.subjects = subjects;
}
public Order() {
}
public Order(OrderType type, LocalDate date, LocalTime time) {
this.type = type;
this.date = date;
this.time = time;
}
// relationship helper methods
public void addSubject(Subject s) {
if (!this.subjects.contains(s)) {
this.subjects.add(s);
}
if (!s.getOrders().contains(this)) {
s.getOrders().add(this);
}
}
public void removeSubject(Subject s) {
if (this.subjects.contains(s)) {
this.subjects.remove(s);
}
if (s.getOrders().contains(this)) {
s.getOrders().remove(this);
}
}
/**
* Get the value of subject
*
* @return the value of subject
*/
/**
* Set the value of subject
*
* @param subject new value of subject
*/
/**
* Get the value of holder
*
* @return the value of holder
*/
public Holder getHolder() {
return holder;
}
/**
* Set the value of holder
*
* @param holder new value of holder
*/
public void setHolder(Holder holder) {
this.holder = holder;
}
@Override
public int hashCode() {
int hash = 7;
hash = 73 * hash + Objects.hashCode(this.id);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Order other = (Order) obj;
if ((this.id == null) || (other.id == null)) {
return false;
}
return Objects.equals(this.id, other.id);
}
/**
* Get the value of time
*
* @return the value of time
*/
public LocalTime getTime() {
return time;
}
/**
* Set the value of time
*
* @param time new value of time
*/
public void setTime(LocalTime time) {
this.time = time;
}
/**
* Get the value of date
*
* @return the value of date
*/
public LocalDate getDate() {
return date;
}
/**
* Set the value of date
*
* @param date new value of date
*/
public void setDate(LocalDate date) {
this.date = date;
}
/**
* Get the value of type
*
* @return the value of type
*/
public OrderType getType() {
return type;
}
/**
* Set the value of type
*
* @param type new value of type
*/
public void setType(OrderType type) {
this.type = type;
}
/**
* Get the value of id
*
* @return the value of id
*/
public Long getId() {
return id;
}
/**
* Set the value of id
*
* @param id new value of id
*/
public void setId(Long id) {
this.id = id;
}
@Override
public String toString() {
return "Order{" + "id=" + id + ", type=" + type + ", date=" + date + ", time=" + time + '}';
}
}
DBInitService.java中的代码是:
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.iit.sat.itmd4515.wren6.service;
import edu.iit.sat.itmd4515.wren6.domain.GenderType;
import edu.iit.sat.itmd4515.wren6.domain.Holder;
import edu.iit.sat.itmd4515.wren6.domain.Order;
import edu.iit.sat.itmd4515.wren6.domain.OrderType;
import edu.iit.sat.itmd4515.wren6.domain.Stock;
import edu.iit.sat.itmd4515.wren6.domain.Subject;
import edu.iit.sat.itmd4515.wren6.domain.SubjectCategory;
import edu.iit.sat.itmd4515.wren6.security.Group;
import edu.iit.sat.itmd4515.wren6.security.GroupService;
import edu.iit.sat.itmd4515.wren6.security.User;
import edu.iit.sat.itmd4515.wren6.security.UserService;
import jakarta.annotation.PostConstruct;
import jakarta.ejb.EJB;
import jakarta.ejb.Singleton;
import jakarta.ejb.Startup;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.logging.Logger;
/**
*
* @author Administrator
*/
@Startup
@Singleton
public class DBInitService {
private static final Logger LOG = Logger.getLogger(DBInitService.class.getName());
@EJB
private HolderService holderSvc;
@EJB
private OrderService orderSvc;
@EJB
private StockService stockSvc;
@EJB
private SubjectService subjectSvc;
@EJB
private GroupService groupSvc;
@EJB
private UserService userSvc;
public DBInitService() {
}
@PostConstruct
private void postContruct() {
LOG.info("DBInitService.postConstruct");
// security realm / indentity store data population
Group adminGroup = new Group("ADMIN_GROUP", "This is a security realm group representing system administrators");
Group holderGroup = new Group("HOLDER_GROUP", "This is a security realm group representing system holders(burrowers)");
Group subjectGroup = new Group("SUBJECT_GROUP", "This is a security realm group representing system subject(operators)");
groupSvc.create(adminGroup);
groupSvc.create(holderGroup);
groupSvc.create(subjectGroup);
User admin = new User("admin", "admin", true);
admin.addGroup(adminGroup);
userSvc.create(admin);
User sub1 = new User("sub1", "sub1", true);
sub1.addGroup(subjectGroup);
sub1.addGroup(adminGroup);
userSvc.create(sub1);
User sub2 = new User("sub2", "sub2", true);
sub2.addGroup(subjectGroup);
userSvc.create(sub2);
User h1 = new User("h1", "h1", true);
h1.addGroup(holderGroup);
userSvc.create(h1);
User h2 = new User("h2", "h2", true);
h2.addGroup(holderGroup);
userSvc.create(h2);
// Create entities and insert sample data
Subject subject1 = new Subject(
"Theory of relativity",
"Albert Einstein",
SubjectCategory.PHYSICS,
"ISBN: 1604132949",
LocalDate.of(1936, 2, 18)
);
subject1.setUser(sub1);
Subject subject2 = new Subject(
"Harry Potter",
"J.K. Rolling",
SubjectCategory.SCIENCE,
"ISBN: 1604232948",
LocalDate.of(2000, 3, 18)
);
subject1.setUser(sub2);
Subject subject3 = new Subject(
"Dark Ducks",
"Douglas Adams",
SubjectCategory.LITERATURE,
"ISBN: 16042325898",
LocalDate.of(2010, 1, 18)
);
Subject subject4 = new Subject(
"Spare",
"Prince Harry",
SubjectCategory.MATHEMATICS,
"ISBN: 9780593593806",
LocalDate.of(2023, 1, 10)
);
Stock stock1 = new Stock(25);
Stock stock2 = new Stock(18);
Stock stock3 = new Stock(36);
Stock stock4 = new Stock(58);
stock1.setSubject(subject1);
stock2.setSubject(subject2);
stock3.setSubject(subject3);
stock4.setSubject(subject4);
stockSvc.create(stock1);
stockSvc.create(stock2);
stockSvc.create(stock3);
stockSvc.create(stock4);
subjectSvc.create(subject1);
subjectSvc.create(subject2);
subjectSvc.create(subject3);
subjectSvc.create(subject4);
Holder holder1 = new Holder(
"Ryan Ren",
"[email protected]",
254854445,
"8618221737351",
GenderType.MALE
);
holder1.setUser(h1);
Holder holder2 = new Holder(
" Elon Musk",
"[email protected]",
29645138,
"1618221731",
GenderType.MALE
);
holder2.setUser(h2);
Holder holder3 = new Holder(
"Alice Walton",
"[email protected]",
296388147,
"161559731",
GenderType.FEMALE
);
holder3.setUser(sub2);
Holder holder4 = new Holder(
"Abigail Johnson",
"[email protected]",
286529185,
"181779364",
GenderType.FEMALE
);
Order order1 = new Order(
OrderType.PRESERVED,
LocalDate.of(2023, 1, 19),
LocalTime.of(01, 19)
);
Order order2 = new Order(
OrderType.RETURNED,
LocalDate.of(2001, 2, 01),
LocalTime.of(18, 21)
);
Order order3 = new Order(
OrderType.PURCHASED,
LocalDate.of(2011, 2, 21),
LocalTime.of(19, 36)
);
Order order4 = new Order(
OrderType.PRESERVED,
LocalDate.of(2022, 4, 12),
LocalTime.of(15, 29)
);
holder1.addOrder(order1);
holder2.addOrder(order2);
holder3.addOrder(order3);
holder4.addOrder(order4);
holderSvc.create(holder1);
holderSvc.create(holder2);
holderSvc.create(holder3);
holderSvc.create(holder4);
order1.setHolder(holder1);
order2.setHolder(holder2);
order3.setHolder(holder3);
order4.setHolder(holder4);
order1.addSubject(subject1);
order2.addSubject(subject2);
order3.addSubject(subject3);
order4.addSubject(subject4);
orderSvc.create(order1);
orderSvc.create(order2);
orderSvc.create(order3);
orderSvc.create(order4);
// write output of persisted sample data - also nevigating relationships
for (Order o : orderSvc.findAll()) {
LOG.info(o.toString());
LOG.info("Order Holder ***************************************");
LOG.info("\t\t" + o.getHolder().toString());
LOG.info("Order Subjects ****************************************");
for (Subject s : o.getSubjects()) {
LOG.info("\t\t" + s.toString());
LOG.info("\t\t\t" + s.getCreator().toString());
LOG.info("\t\t\t" + s.getIdentifier().toString());
LOG.info("\t\t\t" + s.getTitle().toString());
LOG.info("\t\t\t" + s.getCategory().toString());
LOG.info("\t\t\t" + s.getCreateDate().toString());
}
}
for (Holder h : holderSvc.findAll()) {
LOG.info(h.toString());
LOG.info("Holder Orders ************");
for (Order o : h.getOrders()) {
LOG.info("\t\t" + o.toString());
}
}
}
}
我测试了订单实体。如果只创建一行,没有错,只有超过 2 行才会产生这个错误。
我只是定位我的订单实体来查找我的错误。修改validaiton约束后,一切正常。 谢谢你们!!