我有JPA的问题。我无法从数据库中删除人员对象,如下所示:
// trying to delete from my jsp this person-staff (commit error)
Staff del = em.find(Staff.class, staff.getId());
em.getTransaction().begin();
em.remove(del);
em.getTransaction().commit();
em.close();
emf.close();
或更改它:
Staff staff... // get changed data from html form
MyCompositePK temp = new MyCompositePK();
String[] parts = author.split(" ");
temp.setFirstName(parts[0]);
temp.setSurname(parts[1]);
temp.setPatronymic(parts[2]);
Staff change = (Staff) em.find(Staff.class, temp);
em.getTransaction().begin();
change.setId(staff.getId());
em.getTransaction().commit();
em.close();
emf.close();
MyCompositePK是Staff类的嵌入式ID。这是我的实体:
@Embeddable
public class MyCompositePK implements Serializable {
@Column
private String surname;
@Column
private String firstName;
@Column
private String patronymic;
//getters and setters
}
这是员工类声明:
//imports
@Entity
public class Staff {
@EmbeddedId
private MyCompositePK id;
private Date birthday;
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name="POST_ID")
private Post post;
@Lob
@Column(length = 100000)
private byte[] photo;
//getters and setters
}
这是我的文档(我还有另外两个文档,它们继承了Document类),其中包含Staff(ManyToOne)的字段。
package documents;
//imports
@Entity
@Table(name = "incomings")
public class Incoming extends Document {
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumns({ @JoinColumn(name = "sender_firstname", referencedColumnName = "firstname"),
@JoinColumn(name = "sender_surname", referencedColumnName = "surname"),
@JoinColumn(name = "sender_patronymic", referencedColumnName = "patronymic") })
private Staff sender;
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumns({ @JoinColumn(name = "destination_firstname", referencedColumnName = "firstname"),
@JoinColumn(name = "destination_surname", referencedColumnName = "surname"),
@JoinColumn(name = "destination_patronymic", referencedColumnName = "patronymic") })
private Staff destination;
private int outgoingNumber;
private Date outgoingRegDate;
//getters and setters
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Входящий документ" + "\r\n");
sb.append(super.toString());
sb.append("Отправитель" + sender.toString() + "\r\n");
sb.append("Адресат" + destination.toString() + "\r\n");
sb.append("Исходящий номер" + outgoingNumber + "\r\n");
sb.append("Исходящая дата регистрации" + outgoingRegDate.toString() + "\r\n");
sb.append("Текст документа" + super.getContent() + "\r\n");
return sb.toString();
}
}
那么,我可以直接删除Staff对象吗?