Java 无法生成EntityManagerFactory-连接表时出错
以下是我的jpaContext.xml:Java 无法生成EntityManagerFactory-连接表时出错,java,spring,hibernate,Java,Spring,Hibernate,以下是我的jpaContext.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmln
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:annotation-config />
<context:component-scan base-package="com.pluralsight"/>
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="punit"></property>
<property name="dataSource" ref="dataSource"></property>
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"></property>
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.dialect"
value="org.hibernate.dialect.MySQL5InnoDBDialect">
</entry>
<entry key="hibernate.hbm2ddl.auto" value="none"></entry>
<entry key="hibernate.format_sql" value="true"></entry>
</map>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory"
ref="entityManagerFactory">
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://123.123.123.123:1433;databaseName=WikiGenome">
</property>
<property name="username" value="xxx"></property>
<property name="password" value="xxx"></property>
</bean>
</beans>
这是我的另一节课:
@Entity
@Table(name="ChrPosDisease")
public class ChrPosDisease implements Serializable{
@Id
@Column(name="chr")
public String chr;
@Id
@Column(name="pos")
public int pos;
@Id
@Column(name="DiseaseID")
public int diseaseID;
}
我不熟悉hibernate和SpringMVC框架,我只是按照教程中的指导进行操作。
我可以使用以下命令查询结果:
@SuppressWarnings({ "unchecked"})
public List getDiseaseByName(String name) {
Query query = em.createQuery("Select d From Disease d Where d.Name=?1").setParameter(1,name);
List diseaseList=query.getResultList();
return diseaseList;
}
然而,当我通过DiseaseID连接两个表时,它给出了以下错误
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [Select d From cuhk.cse.fyp.model.Disease d JOIN ChrPosDisease c Where d.DiseaseID=c.DiseaseID AND d.Name=?1]
我没有在仅使用一个表查询结果时出错
下面是我用来连接这两个表的代码:
@SuppressWarnings({ "unchecked"})
public List getJoinDiseaseByName(String name) {
Query query = em.createQuery("Select d From Disease d JOIN ChrPosDisease c Where d.DiseaseID=c.DiseaseID AND d.Name=?1").setParameter(1,name);
List diseaseList=query.getResultList();
return diseaseList;
}
怎么了
谢谢你的帮助
补充:
最新ChrPosDisease
@SuppressWarnings("serial")
@Entity
@Table(name="ChrPosDisease")
public class ChrPosDisease implements Serializable{
@Id
@Column(name="chr")
public String chr;
@Id
@Column(name="pos")
public int pos;
@Column(name="DiseaseID")
public int diseaseID;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="DiseaseID",nullable=false)
private Disease disease;
}
最新疾病:
@SuppressWarnings("serial")
@Entity
@Table(name="Disease")
public class Disease implements Serializable{
@Id
@GeneratedValue
@Column(name="DiseaseID")
public int DiseaseID;
@Column(name="Name")
public String Name;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "ChrPosDisease")
private Set<ChrPosDisease> chrPosDisease = new HashSet<ChrPosDisease>();
}
@SuppressWarnings(“串行”)
@实体
@表(name=“Disease”)
公共类实现可序列化{
@身份证
@生成值
@列(name=“DiseaseID”)
公共疾病;
@列(name=“name”)
公共字符串名称;
@OneToMany(fetch=FetchType.LAZY,mappedBy=“ChrPosDisease”)
私有集chrPosDisease=新HashSet();
}
我使用了上述实体,但在部署时存在异常
是否需要添加其他内容?我认为您不应该使用d.ChrPosDisease,而应该仅使用ChrPosDisease,因为d是唯一疾病实体的别名 它应该会起作用
如果不起作用,请告诉我。您应该将映射条目设置为:
@OneToMany(fetch = FetchType.LAZY, mappedBy = "disease")
private Set<ChrPosDisease> chrPosDisease = new HashSet<ChrPosDisease>();
@OneToMany(fetch=FetchType.LAZY,mappedBy=“disease”)
私有集chrPosDisease=新HashSet();
mappedBy
属性通知该字段由该特定实体属性映射。因此,这个属性应该是连接manytone
侧的实体的属性
这里的
Disease
有许多ChrPosDisease
。而ChrPosDisease
有一种Disease
。因此,“按列映射”应该是OneToMany
字段与ManyToOne
侧的属性绑定的列。您指的是哪一个?1.从疾病d中选择d,加入疾病c,其中d.DiseaseID=c.DiseaseID,d.Name=?1 2。从疾病d JOIN CHRPOSDISSIDE d中选择d,其中d.Name=?1Query query=em.createQuery(“从疾病d JOIN d d中选择d,其中d.DiseaseID=c.DiseaseID,d.Name=?1”)。setParameter(1,名称);在上面的一行中,我们可以看到您有d.ChrPosDisease,所以ChrPosDisease是一个表,您如何使用别名作为表?把它拿开,试着让我知道我只是在跟踪。请搜索“导航到单值关系字段”。我也对这种语法感到困惑。谢谢你的帮助,我发现这个异常是因为我在ChrPosDisease.class中没有“@Id”造成的。添加“@Id”后,出现语法错误异常。java.lang.IllegalArgumentException:org.hibernate.hql.internal.ast.QuerySyntaxException:预期加入的路径![从中大选择d.cse.fyp.model.Disease d加入ChrPosDisease c,其中d.DiseaseID=c.DiseaseID和d.Name=?1]谢谢您的解释!
@OneToMany(fetch = FetchType.LAZY, mappedBy = "disease")
private Set<ChrPosDisease> chrPosDisease = new HashSet<ChrPosDisease>();