67,538
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="YingShe_PU">
<jta-data-source>java:/yingshe_MySqlDS</jta-data-source>
<class>huizhi.entity.Person</class>
<class>huizhi.entity.Card</class>
</persistence-unit>
</persistence>
package huizhi.entity;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name="person")
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String address;
private int age;
private String name;
private String phone;
private Card card;
public Person() {
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(unique=true, nullable=false)
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@Column(length=50)
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
@Column(nullable=false, length=20)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(length=15)
public String getPhone() {
return this.phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
//bi-directional one-to-one association to Card
@OneToOne(cascade={CascadeType.REFRESH})
@JoinColumn(name="cardid", referencedColumnName="cid")
public Card getCard() {
return this.card;
}
public void setCard(Card card) {
this.card = card;
}
}
package huizhi.entity;
import java.io.Serializable;
import javax.persistence.*;
import java.util.Date;
@Entity
@Table(name="card")
public class Card implements Serializable {
private static final long serialVersionUID = 1L;
private String cid;
private Date valDate;
private Person person;
public Card() {
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="Cid", unique=true, nullable=false, length=18)
public String getCid() {
return this.cid;
}
public void setCid(String cid) {
this.cid = cid;
}
@Temporal( TemporalType.DATE)
@Column(name="ValDate", nullable=false)
public Date getValDate() {
return this.valDate;
}
public void setValDate(Date valDate) {
this.valDate = valDate;
}
//bi-directional one-to-one association to Person
@OneToOne(mappedBy="card", cascade={CascadeType.MERGE, CascadeType.REMOVE}, optional=false)
public Person getPerson() {
return this.person;
}
public void setPerson(Person person) {
this.person = person;
}
}
@PersistenceContext(unitName="YingShe_PU")protected EntityManager em;
public void addPerson(String name, int age, String phone, String address, String cid, Date valDate) {
Person p = new Person();
Card c = new Card();
c.setCid(cid);
c.setValDate(valDate);
c.setPerson(p);
p.setId(1);
p.setName(name);
p.setAddress(address);
p.setAge(age);
p.setPhone(phone);
p.setCard(c);
try{
//em.persist(c);
em.persist(p);
}catch(Exception e){
System.err.println(e.getMessage());
}
}
<%
DateFormat format= new SimpleDateFormat("yyyy-MM-dd");
InitialContext ctx = new InitialContext();
PersonbeanLocal pl = (PersonbeanLocal)ctx.lookup("Personbean");
out.println("<br>---------------添加个人信息------------<br>");
//添加一个个人信息
pl.addPerson("李小婉",20,"13526606880","北京市朝阳区安定路","410182198807189875",format.parse("2020-12-12"));
%>
16:52:54,765 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
org.hibernate.PersistentObjectException: detached entity passed to persist: huizhi.entity.Person
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:102)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:645)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:619)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:623)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:220)
at org.jboss.jpa.tx.TransactionScopedEntityManager.persist(TransactionScopedEntityManager.java:187)
at huizhi.sessionbean.Personbean.addPerson(Personbean.java:37)