我有以下实体类:
@Entity
@Table(name="TBL_SPJ_PEOPLE")
public class People implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="PEOPLE_ID", unique=true)
private int personID = 0;
@Column(name="PEOPLE_NAME")
private String name = "";
@Column(name="PEOPLE_PROFILEPATH")
private String profilePath = "";
@Column(name="PEOPLE_PERSON_TYPE")
private String personType = "";
@Column(name="PEOPLE_DEPARTMENT")
private String department = "";
@Column(name="PEOPLE_JOB")
private String job = "";
@Column(name="PEOPLE_ORDER")
private int order = 0;
@Column(name="PEOPLE_ADULT")
private boolean adult = false;
@Column(name="PEOPLE_AKA")
private String alsoKnownAs = "";
@Lob
@Column(name="PEOPLE_BIOGRAPHY")
private String biography = "";
@Temporal(TemporalType.DATE)
@Column(name="PEOPLE_BIRTHDAY")
private Date birthday = null;
@Temporal(TemporalType.DATE)
@Column(name="PEOPLE_DEADTHDAY")
private Date deathday = null;
@Column(name="PEOPLE_HOMEPAGE")
private String homepage = "";
@Column(name="PEOPLE_BIRTHPLACE")
private String birthPlace = "";
@ManyToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@JoinTable(name="TBL_SPJ_PEOPLE_CAST", joinColumns={@JoinColumn(name="PEOPLE_ID")}, inverseJoinColumns={@JoinColumn(name="CAST_PEOPLE_ID")})
private List<CastPeople> casts = null;
@ManyToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@JoinTable(name="TBL_SPJ_PEOPLE_CREW", joinColumns={@JoinColumn(name="PEOPLE_ID")}, inverseJoinColumns={@JoinColumn(name="CREW_PEOPLE_ID")})
private List<CrewPeople> crews = null;
@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@JoinColumn(name="PEOPLE_ID", referencedColumnName="PEOPLE_ID")
private List<Image> images = null;
public People() {}
@SuppressWarnings("unchecked")
@Transient
public List<String> getPeople() {
List<String> people = new ArrayList<String>();
EntityManagerFactory factory = Persistence.createEntityManagerFactory("SPJUnit");
EntityManager em = factory.createEntityManager();
Query query = em.createQuery("SELECT p.name FROM People p");
people = query.getResultList();
return people;
}
public int getPersonID() {
return personID;
}
public void setPersonID(int personID) {
this.personID = personID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProfilePath() {
return profilePath;
}
public void setProfilePath(String profilePath) {
this.profilePath = profilePath;
}
public String getPersonType() {
return personType;
}
public void setPersonType(String personType) {
this.personType = personType;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
public boolean isAdult() {
return adult;
}
public void setAdult(boolean adult) {
this.adult = adult;
}
public String getAlsoKnownAs() {
return alsoKnownAs;
}
public void setAlsoKnownAs(String alsoKnownAs) {
this.alsoKnownAs = alsoKnownAs;
}
public String getBiography() {
return biography;
}
public void setBiography(String biography) {
this.biography = biography;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Date getDeathday() {
return deathday;
}
public void setDeathday(Date deathday) {
this.deathday = deathday;
}
public String getHomepage() {
return homepage;
}
public void setHomepage(String homepage) {
this.homepage = homepage;
}
public String getBirthPlace() {
return birthPlace;
}
public void setBirthPlace(String birthPlace) {
this.birthPlace = birthPlace;
}
public List<CastPeople> getCasts() {
return casts;
}
public void setCasts(List<CastPeople> casts) {
this.casts = casts;
}
public List<CrewPeople> getCrews() {
return crews;
}
public void setCrews(List<CrewPeople> crews) {
this.crews = crews;
}
public List<Image> getImages() {
return images;
}
public void setImages(List<Image> images) {
this.images = images;
}
}
这个persistance.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence 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_1_0.xsd"
version="1.0">
<persistence-unit name="SPJUnit" transaction-type="JTA">
<jta-data-source>jdbc/peoplesDB</jta-data-source>
<class>de.hof.university.spj.beans.People</class>
</persistence-unit>
我在这里使用了entitymanager:
public void buildDatabase() throws IOException, SAXException {
logger.info("Set up Database.");
if(factory == null) {
factory = Persistence.createEntityManagerFactory("SPJUnit");
}
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Query query = em.createQuery("SELECT p.title FROM Person p");
boolean importData = (query.getResultList().size() == 0);
if(importData) {
this.importData(em);
} else {
logger.info("Data already imported.");
}
em.getTransaction().commit();
em.close();
}
private void importData(EntityManager em) throws SAXException, IOException {
logger.info("Import Data into Database.");
XMLParser myParser = new XMLParser();
List<People> allPeoples = myParser.parsePeople("people.xml", new XMLPeopleHandler());
for(People people: allPeoples) {
logger.info("Persist Person: " + people.getName());
em.persist(people);
}
}
我收到以下错误:
java.lang.IllegalArgumentException: Object: People is not a known entity type
如果我不在我的persistence.xml中使用jta而是设置它:
<properties>
<property name="persistence-context.persist-on-commit" value="true"/>
<property name="hibernate.connection.url" value="jdbc:h2:./data/datastore" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"></property>
<property name="hibernate.connection.driver_class" value="org.h2.Driver" />
<property name="hibernate.connection.password" value="admin" />
<property name="hibernate.connection.username" value="admin" />
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
一切正常......所以它必须是jta的问题?