我可以使用通过postman发送的JSON格式使用没有外键的表向数据库添加值,但是,在插入外键时我收到错误。有人可以发一个代码来解决这个问题我完全不知道如何解决它
这是我的错误:
private void Acc_Button_createActionPerformed(java.awt.event.ActionEvent evt) {
int p = JOptionPane.showConfirmDialog(rootPane, "Do you want add", "Add", JOptionPane.YES_NO_OPTION);
if (p == 0) {
try {
String sql = "insert into ACCOUNT_INFO (ACC,NAMEF,DOB,PIN,ACCOUNT_TPAYE,NATIONALTY,CASTE,MICR_NO,GENDER,MOB_NO,ADDRESS,SEC_Q,SEC_A,BLANCE)values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
jrowSet.setCommand(sql);
jrowSet.setString(1, Acc_TextField_acc.getText());
jrowSet.setString(2, Acc_TextField_amunt.getText());
jrowSet.setString(3, ((JTextField) Acc_DateChooser_birh.getDateEditor().getUiComponent()).getText());
jrowSet.setString(4, Acc_TextField_pin.getText());
jrowSet.setString(5, (String) Acc_ComboBox_acc_type.getSelectedItem());
jrowSet.setString(6, (String) Acc_ComboBox_naton.getSelectedItem());
jrowSet.setString(7, Acc_TextField_name.getText());
jrowSet.setString(8, Acc_TextField_misr.getText());
// for radio btn
RadioButton_male.setActionCommand("Male");
RadioButton_female.setActionCommand("Femal");
// to insert radin in database
jrowSet.setString(9, buttonGroup2.getSelection().getActionCommand());
jrowSet.setString(10, Acc_TextField_caste.getText());
jrowSet.setString(11, Acc_TextField_adrres.getText());
jrowSet.setString(12, (String) Acc_ComboBox_sq_q.getSelectedItem());
jrowSet.setString(13, Acc_TextField_mob_no.getText());
jrowSet.setString(14, Acc_TextField_seq_a.getText());
jrowSet.execute();
// Bal();
JOptionPane.showMessageDialog(rootPane, "added", "add acount", JOptionPane.INFORMATION_MESSAGE);
} catch (HeadlessException | SQLException e) {
}
}
}
// Acc_Button_createActionPerformed(Account.java:414) >> this is the ine 414
jrowSet.setString(9, buttonGroup2.getSelection().getActionCommand());
我正在插入:
{
“acceptedGender”: “既”,
“价格”:123123.00,
“类型”: “公寓”,
“vacantNum”:13,
“HADID”:4个
}
我使用下面的post方法插入
有人可以帮助我
这是我的代码:
HTTP Status 500 - javax.persistence.RollbackException: java.lang.IllegalStateException:
During synchronization a new object was found through a relationship that was not marked
cascade PERSIST: com.balay.entity.HaDetails[ hadID=null ].
@javax.ejb.Stateless
@Path("com.balay.entity.hatypes")
public class HaTypesFacadeREST extends AbstractFacade<HaTypes> {
@PersistenceContext(unitName = "BalayRSPU")
private EntityManager em;
public HaTypesFacadeREST() {
super(HaTypes.class);
}
**@POST
@Override
@Consumes({MediaType.APPLICATION_JSON})
public void create(HaTypes entity) {
em =
Persistence.createEntityManagerFactory("BalayRSPU").createEntityManager();
try{
em.getTransaction().begin();
em.persist(entity);
em.getTransaction().commit();
}finally{
em.close();
}
}**
@PUT
@Path("{id}")
@Consumes({MediaType.APPLICATION_JSON})
public void edit(@PathParam("id") Integer id, HaTypes entity) {
super.edit(entity);
}
@DELETE
@Path("{id}")
public void remove(@PathParam("id") Integer id) {
super.remove(super.find(id));
}
@GET
@Path("{id}")
@Produces({MediaType.APPLICATION_JSON})
public HaTypes find(@PathParam("id") Integer id) {
return super.find(id);
}
@GET
@Override
@Produces({MediaType.APPLICATION_JSON})
public List<HaTypes> findAll() {
return super.findAll();
}
@GET
@Path("{from}/{to}")
@Produces({MediaType.APPLICATION_JSON})
public List<HaTypes> findRange(@PathParam("from") Integer from,
@PathParam("to") Integer to) {
return super.findRange(new int[]{from, to});
}
@GET
@Path("count")
@Produces(MediaType.TEXT_PLAIN)
public String countREST() {
return String.valueOf(super.count());
}
@Override
protected EntityManager getEntityManager() {
em =
Persistence.createEntityManagerFactory("BalayRSPU").createEntityManager();
return em;
}
}
@Entity
@Table(name = "ha_types")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "HaTypes.findAll", query = "SELECT h FROM HaTypes h")
, @NamedQuery(name = "HaTypes.findByHatID", query = "SELECT h FROM HaTypes h
WHERE h.hatID = :hatID")
, @NamedQuery(name = "HaTypes.findByType", query = "SELECT h FROM HaTypes h
WHERE h.type = :type")
, @NamedQuery(name = "HaTypes.findByAcceptedGender", query = "SELECT h FROM
HaTypes h WHERE h.acceptedGender = :acceptedGender")
, @NamedQuery(name = "HaTypes.findByVacantNum", query = "SELECT h FROM
HaTypes h WHERE h.vacantNum = :vacantNum")
, @NamedQuery(name = "HaTypes.findByPrice", query = "SELECT h FROM HaTypes h
WHERE h.price = :price")})
public class HaTypes implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "hatID")
private Integer hatID;
@Basic(optional = false)
@Column(name = "type")
private String type;
@Basic(optional = false)
@Column(name = "acceptedGender")
private String acceptedGender;
@Basic(optional = false)
@Column(name = "vacantNum")
private int vacantNum;
// @Max(value=?) @Min(value=?)//if you know range of your decimal fields
consider using these annotations to enforce field validation
@Basic(optional = false)
@Column(name = "price")
private BigDecimal price;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "hatID")
private Collection<Reservation> reservationCollection;
@JoinColumn(name = "hadID", referencedColumnName = "hadID")
@ManyToOne(optional = false)
private HaDetails hadID;
public HaTypes() {
}
public HaTypes(Integer hatID) {
this.hatID = hatID;
}
public HaTypes(Integer hatID, String type, String acceptedGender, int
vacantNum, BigDecimal price) {
this.hatID = hatID;
this.type = type;
this.acceptedGender = acceptedGender;
this.vacantNum = vacantNum;
this.price = price;
}
public Integer getHatID() {
return hatID;
}
public void setHatID(Integer hatID) {
this.hatID = hatID;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getAcceptedGender() {
return acceptedGender;
}
public void setAcceptedGender(String acceptedGender) {
this.acceptedGender = acceptedGender;
}
public int getVacantNum() {
return vacantNum;
}
public void setVacantNum(int vacantNum) {
this.vacantNum = vacantNum;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
@XmlTransient
public Collection<Reservation> getReservationCollection() {
return reservationCollection;
}
public void setReservationCollection(Collection<Reservation>
reservationCollection) {
this.reservationCollection = reservationCollection;
}
public HaDetails getHadID() {
return hadID;
}
public void setHadID(HaDetails hadID) {
this.hadID = hadID;
}
@Override
public int hashCode() {
int hash = 0;
hash += (hatID != null ? hatID.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are
not set
if (!(object instanceof HaTypes)) {
return false;
}
HaTypes other = (HaTypes) object;
if ((this.hatID == null && other.hatID != null) || (this.hatID != null
&& !this.hatID.equals(other.hatID))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.balay.entity.HaTypes[ hatID=" + hatID + " ]";
}
}