2
我在使用JAXB將JSON字符串解組到我的JPA實體中的JAX-RS服務中遇到問題。編組的作品和我從服務的以下JSON字符串:使用JAXB解組JPA實體
{
"id":1,
"items":[{
"id":{"pairName":"PAIR-1","eventId":1},
"pair":{"name":"PAIR-1","val":1.0},"quantity":2}
]}
}
然而,發送此字符串返回到我的服務提供了一個錯誤:
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: Column EVENT_ID' cannot accept a NULL value.
Error Code: -1
Call: INSERT INTO ITEM (QUANTITY, EVENT_ID, PAIR_NAME) VALUES (?, ?, ?) bind => [2, null, PAIR-1]
EVENT_ID是字符串中,我怎麼弄物體回來了嗎?我使用Java EE 5,JPA 1.0與Jersey和EclipseLink。
JPA實體
@Entity
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Event implements Serializable {
@Id
private long id;
@OneToMany(mappedBy = "event")
@XmlInverseReference(mappedBy = "event")
private List<Item> items;
}
@Entity
@XmlAccessorType(XmlAccessType.FIELD)
public class Item implements Serializable {
@EmbeddedId
private ItemPk id;
@ManyToOne(cascade = { CascadeType.ALL })
private Pair pair;
@ManyToOne
@JoinColumn(name = "EVENT_ID")
@XmlTransient
private Event event;
private int quantity;
}
@Embeddable
@XmlAccessorType(XmlAccessType.FIELD)
public class ItemPk implements Serializable {
@Column(name="PAIR_NAME", insertable=false, updatable=false)
private String pairName;
@Column(name="EVENT_ID", insertable=false, updatable=false)
private long eventId;
}
@Entity
public class Pair implements Serializable {
@Id
private String name;
private float val;
}
JAX-RS服務
@Path("/events")
public class EventService {
private EntityManagerFactory entityManagerFactory;
@SuppressWarnings("unchecked")
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Event> list() {
if (entityManagerFactory == null) {
entityManagerFactory = Persistence.createEntityManagerFactory("service");
}
EntityManager entityManager = entityManagerFactory.createEntityManager();
Query query = entityManager.createNamedQuery("Event.findAll");
return query.getResultList();
}
@PUT
@Consumes(MediaType.APPLICATION_JSON)
public void update(Event event) {
if (entityManagerFactory == null) {
entityManagerFactory = Persistence.createEntityManagerFactory("service");
}
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.merge(event);
entityManager.getTransaction().commit();
}
}