我尝试保留@OneToMany,但出现错误:
插入
into booking (booking_date, cancel_date) values (?, ?)
将参数[1]绑定为[DATE]-[2018-07-20]
将参数[2]绑定为[DATE]-[null]
org.hibernate.SQL:
insert into category_booking (name) values (?)
com.arjuna.ats.jta:ARJUNA016060:TransactionImple.enlistResource- 捕获:XAException.XAER_INVAL
o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:0,SQLState: 空
o.h.engine.jdbc.spi.SqlExceptionHelper: ConnectionImple.registerDatabase-ARJUNA017017:资源清单 失败
o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:0,SQLState: 空
o.h.engine.jdbc.spi.SqlExceptionHelper:ARJUNA017005:错误 发生在关闭期间:
com.arjuna.ats.jta:ARJUNA016029: SynchronizationImple.afterCompletion-失败
org.hibernate.resource.transaction.backend.jta.internal.synchronization.RegisteredSynchronization@5c66658b 例外
org.hibernate.exception.GenericJDBCException:无法发布JDBC 连接
这是我的实体:
@Entity
@Table(name="booking")
public class Booking implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private Long id;
@Column(name="booking_date")
private Date bookingDate;
@Column(name="cancel_date")
private Date cancelDate;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "booking_id")
private List<CategoriesBooking> bookingInfos = new ArrayList<>();
}
@Entity
@Table(name="category_booking")
public class CategoriesBooking implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private Long id;
@Column(name = "name")
private String name;
}
持久功能
@Repository
public class ServicesRepos{
@PersistenceContext
protected EntityManager entityManager;
public AbstractServices() {
}
public EntityManager getEm() {
return entityManager;
}
@Transactional
@SuppressWarnings("unchecked")
public Object add(Object entity){
try {
EntityManager em = getEm();
em.persist(entity);
return entity;
} catch (Exception e) {
entityManager.getTransaction().rollback();
}
return null;
}
}
和我的输入数据:
{
bookingdate: 2018-07-20,
bookingInfos: [{
name: "test for persist"
}]
}