这是我的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"
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>
这是我的Disease.java:
@Entity @Table(name="Disease") public class Disease { @Id @GeneratedValue @Column(name="DiseaseID") public int DiseaseID; @Column(name="Name") public String Name; }
这是我的另一个课程:
@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和spring mvc框架的新手,我只是按照教程中的指南进行操作。 我可以使用:
查询结果@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>();
}
我使用了上面的实体,部署时有例外。
我还需要添加其他内容吗?
答案 0 :(得分:0)
我认为你不应该使用d.ChrPosDisease而只使用ChrPosDisease,因为d只是疾病实体的别名。
它应该有用。
让我知道它是否无效。
答案 1 :(得分:0)
您应该将映射条目设为:
@OneToMany(fetch = FetchType.LAZY, mappedBy = "disease")
private Set<ChrPosDisease> chrPosDisease = new HashSet<ChrPosDisease>();
mappedBy
属性通知该字段由该特定实体属性映射。因此,此属性应该是ManyToOne
侧连接实体的属性。
此处Disease
有许多ChrPosDisease
。 ChrPosDisease
有一个Disease
。因此,按列映射的应该是OneToMany
字段与ManyToOne
侧的属性绑定的字段。