我是Java EE的新手,我使用教程jpa advance mapping是为了更好地理解JPA,尤其是“联接表策略”。但是,每当我运行建议该教程的内容时,都会出现错误:
Table 'testdb.TeachingStaff' doesn't exist
/jpa.xhtml @263,82 value="#{jpa.teachingStaffJoinTableStrategy}": javax.faces.convert.ConverterException: Transaction error during loading:org.hibernate.exception.SQLGrammarException: could not extract ResultSet
我正在使用的是jsf呈现UI,jpa.java中的托管bean由jsf,jboss 6.4,mysql 5.7.24,hibernate 4.2.18管理。
在persistence.xml中,我具有以下内容:
<?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 = "JoinedTableStrategyAdvancedMappings" transaction-type = "RESOURCE_LOCAL">
<class>jpa.joinTableStrategy.Staff</class>
<class>jpa.joinTableStrategy.NonTeachingStaff</class>
<class>jpa.joinTableStrategy.TeachingStaff</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/testdb"/>
<property name="hibernate.connection.driver_class" value="com.mysql.cj.jdbc.Driver"/>
<property name="hibernate.connection.username" value="myusername"/>
<property name="hibernate.connection.password" value="mypass"/>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hbm2ddl.auto" value="update"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.DB2400Dialect"/>
</properties>
</persistence-unit>
</persistence>
在JSF文件jpa.xhmtl中,我有:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:tp = "http://tutorialspoint.com/facelets"
xmlns:cc="http://java.sun.com/jsf/composite/components">
<h:head>
<title>JPA</title>
</h:head>
<h:body>
<h:dataTable value="#{jpa.teachingStaffJoinTableStrategy}" var="staff"
styleClass = "userTable"
headerClass = "userTableHeader"
rowClasses = "userTableOddRow,userTableEvenRow">
<h:column>
<f:facet name = "header">ID</f:facet>
#{staff.sid}
</h:column>
<h:column>
<f:facet name = "header">Type</f:facet>
#{staff.type}
</h:column>
<h:column>
<f:facet name = "header">Name</f:facet>
#{staff.sname}
</h:column>
<h:column>
<f:facet name = "header">Qaulification</f:facet>
#{staff.qualification}
</h:column>
<h:column>
<f:facet name = "header">Subject Expertise</f:facet>
#{staff.subjectexpertise}
</h:column>
</h:dataTable>
</h:body>
</html>
在jpa.java中,我有:
@ManagedBean(name = "jpa", eager = true)
@SessionScoped
public class jpa implements Serializable {
private HibernateUtil utilJoinTableStrategy = new HibernateUtil("JoinedTableStrategyAdvancedMappings");
public List<TeachingStaff> getTeachingStaffJoinTableStrategy(){
List<TeachingStaff> result;
try{
result = (List<TeachingStaff>) this.utilJoinTableStrategy.createQuery("select e from TeachingStaff e"); // here there is the exception that could not extract ResultSet
}catch (Exception e){
FacesMessage msg = new FacesMessage("Transaction error during loading:"+e.getMessage());
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ConverterException(msg);
}
return result;
}
}
HibernateUtil.java具有:
public class HibernateUtil {
private EntityManagerFactory entityManagerFactory;
private EntityManager entityManager = null;
public HibernateUtil(String persistenceUnitName){
this.entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName);
}
private void createEntityManager(){
if (this.entityManager==null){
this.entityManager = entityManagerFactory.createEntityManager();
}
}
private void closeConnection(){
this.entityManager.close();
this.entityManager = null;
}
List createQuery(String query) throws Exception{
this.createEntityManager();
List result;
try{
result = this.entityManager.createQuery(query).getResultList();
}catch (Exception e){
throw new Exception(e.getMessage());
}
return result;
}
}
然后对于实体,我具有以下内容: Staff.java
package jpa.joinTableStrategy;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name = "staff", schema = "testdb", catalog = "")
@Inheritance( strategy = InheritanceType.JOINED )
public class Staff implements Serializable {
@Id
@GeneratedValue( strategy = GenerationType.AUTO )
private int sid;
@Basic
@Column(name = "sname")
private String sname;
@Basic
@Column(name = "type")
private String type;
public Staff( int sid, String sname, String type ) {
super( );
this.sid = sid;
this.sname = sname;
this.type = type;
}
public Staff( ) {
super( );
}
public int getSid( ) {
return sid;
}
public void setSid( int sid ) {
this.sid = sid;
}
public String getSname( ) {
return sname;
}
public void setSname( String sname ) {
this.sname = sname;
}
public void setType( String type){
this.type = type;
}
public String getType (){
return type;
}
}
TeachingStaff.java
package jpa.joinTableStrategy;
import javax.persistence.*;
@Entity
@PrimaryKeyJoinColumn(referencedColumnName="sid")
public class TeachingStaff extends Staff {
@Basic
@Column(name = "qualification")
private String qualification;
@Basic
@Column(name = "subjectexpertise")
private String subjectexpertise;
public TeachingStaff( int sid, String sname, String qualification,String subjectexpertise ) {
super( sid, sname, "TS" ); // I added the TS but not sure if it's correct
this.qualification = qualification;
this.subjectexpertise = subjectexpertise;
}
public TeachingStaff( ) {
super( );
}
public String getQualification( ){
return qualification;
}
public void setQualification( String qualification ){
this.qualification = qualification;
}
public String getSubjectexpertise( ) {
return subjectexpertise;
}
public void setSubjectexpertise( String subjectexpertise ){
this.subjectexpertise = subjectexpertise;
}
}
和NonTeachingStaff.java
package jpa.joinTableStrategy;
import javax.persistence.*;
@Entity
@PrimaryKeyJoinColumn(referencedColumnName="sid")
public class NonTeachingStaff extends Staff {
@Basic
@Column(name = "areaexpertise")
private String areaexpertise;
public NonTeachingStaff( int sid, String sname, String areaexpertise ) {
super( sid, sname, "NS" );
this.areaexpertise = areaexpertise;
}
public NonTeachingStaff( ) {
super( );
}
public String getAreaexpertise( ) {
return areaexpertise;
}
public void setAreaexpertise( String areaexpertise ) {
this.areaexpertise = areaexpertise;
}
}
然后表人员有以下几列
我不确定是怎么回事。
预先感谢