我正在使用集成在 MySQL Workbench 中的数据库“world”
我尝试过提交事务,但出现了很长的错误
怎么了?
import javax.persistence.*;
public class MyPC{
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("test_persistence");
EntityManager em = emf.createEntityManager();
System.out.println("Start hibernate test");
em.getTransaction().begin();
city Mon = new city(15000, "FFF", "BBB", "xktyjdj", 35000);
em.persist(Mon);
em.getTransaction().commit();
System.out.println("New ID is "+Mon.getID());
}
}
city.java
import javax.persistence.Entity;
import javax.persistence.*;
@Entity
@Table(name = "city")
public class city{
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int ID;
@Column(name = "Name")
private String Name;
@Column(name = "CountryCode")
private String CountryCode;
@Column(name = "District")
private String District;
@Column(name = "Population")
private int Population;
public city(int id, String name, String countrycode, String district, int population){
ID = id;
Name = name;
CountryCode = countrycode;
District = district;
Population = population;
}
public int getID(){
return ID;
}
public String getname(){
return Name;
}
}
这个错误
线程“main”中的异常javax.persistence.PersistenceException:org.hibernate.PersistentObjectException:分离的实体传递给persist:city 在 org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154) 在 org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181) 在 org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188) 在 org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:726) 在 org.hibernate.internal.SessionImpl.persist(SessionImpl.java:706) 在 MyPC.main(MyPC.java:11) 引起:org.hibernate.PersistentObjectException:分离的实体传递给持久化:城市 在 org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:120) 在 org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:55) 在 org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:93) 在 org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:720) ... 2个