I use spring data jpa in my web-app, i have entity user
#Entity
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String password;
private String email;
private Boolean enabled;
private String name;
private String lastname;
private String userRole;
public User() {
}
public User(String password, String email, Boolean enabled, String name, String lastname, String userRole) {
this.password = password;
this.email = email;
this.enabled = enabled;
this.name = name;
this.lastname = lastname;
this.userRole = userRole;
}
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = "users_id_seq")
#SequenceGenerator(name="users_id_seq", sequenceName="users_id_seq", allocationSize = 1)
#Column(name = "id", nullable = false)
public Long getId() {
return id;
}
//Other columns
}
And i have UserRepository interface which extends CrudRepository.
When i call method findAll in my Controller, I get this error
01-May-2016 22:45:58.674 WARN [http-nio-8080-exec-3] org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions SQL Error: 0, SQLState: 42703
01-May-2016 22:45:58.675 ERROR [http-nio-8080-exec-3] org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions Error: column user0_.id does not exist
Position: 8
My spring-config.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:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<jpa:repositories base-package="com.birthright.repository"/>
<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.birthright.entity"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<tx:annotation-driven/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/AutoService"/>
<property name="username" value="postgres"/>
<property name="password" value="root"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEmf"/>
</bean>
</beans>
My table in postgresql
CREATE TABLE public."user"
(
id integer NOT NULL DEFAULT nextval('users_id_seq'::regclass),
password character varying,
email character varying,
enabled boolean,
name character varying,
lastname character varying,
user_role character varying,
CONSTRAINT users_pkey PRIMARY KEY (id)
)
User is a reserved keyword in PostgreSQL. With a default naming strategy you, probably, had User table name.
Don't know why #Table(name = "user", schema = "public") works. Maybe PostgreSQL doesn't consider public.user as a keyword opposite User.
Please use plural names for tables. And using a system or subsystem prefix for a table name (xxx_users) is a good idea too.
A naming strategy can be used for such approach. Refer this as an example: Hibernate5NamingStrategy
An example of prefixes:
StrategyOptions
Related
I am not able to auto-create table from maven spring mvc.
I was doing a spring mvc project using maven. By far i have got no errors but when i am trying to create table in my database using applicationconfig.xml it is not working. i have searched over the internet but my applicationconfig.xml seems fine to me. I have got no errors. Still the table is not created..
applicationconfig.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<bean id="entityManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.milan.entities" /><!--scans model/entity/domain(name 3 but same) and registers-->
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/inventorymanagement" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManager" />
</bean>
<tx:annotation-driven />
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
</beans>
User Class
package com.milan.entities;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
public class User implements Serializable{
#Id
#Column(name = "USER_ID")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long userId;
#Column(name = "USERNAME")
private String userName;
#Column(name = "PASSWORD")
private String password;
#Column(name = "IS_ENABLEd")
private boolean isEnabled;
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isIsEnabled() {
return isEnabled;
}
public void setIsEnabled(boolean isEnabled) {
this.isEnabled = isEnabled;
}
}
There is no error while running the program however the table is not created automatically.
Put #Entity on your User class, if the table still not created try putting below annotations on the class:
#Entity
#Table(name = "`user`")
Could be happening because User is a reserve word in DB.
I have an issue with my project using Spring (4.2.4 Release) and JPA (2.1)
To be brief the problem is in the part of code in file "Book.java":
#Table(name = "book")
#NamedQueries({
#NamedQuery(name = "Book.getBooks", query="select b from com.jpaProSpring.Book b ")
})
#Entity(name = "Book")
public class Book implements Serializable {
private int id;
private String title;
private String description;
public Book() {
}
#Id
#GeneratedValue(strategy = IDENTITY)
#Column(name = "id")
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
#Column
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
#Column
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
}
and particulary in
query = "select b from com.jpaProSpring.Book b" )
My IntelliJ highlights the Book and says that class is not en entity. And I have no idea, why it is so given that I was doing an example from the book.
Link to my project https://github.com/yuraguz/LearnORM.git
My spring-config.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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3310/testdb"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
<property name="initialSize" value="5"/>
<property name="maxActive" value="10"/>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf" />
</bean>
<bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="packagesToScan" value="com.jpaProSpring" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<context:annotation-config />
<context:component-scan base-package="com.jpaProSpring" />
</beans>
P.S: I know entities must be declared in persistence.xml in META-INF/ source. But if I understand correctly Spring4 make it possible to config project without persistance.xml. So, I don't use it.
If you use InellyJ Idea, possible you didn't add JPA facet in your project. Try open menu "File"->"Project Structure", then select in list of Project Settings section named "Facets". Check if you have configured JPA facet in list of facets. If you don't see JPA facet, just add it (by pressing "+" button). Optionally, you can specify persistence.xml (if you have it) and Default JPA provider. Hope this will help.
I try get data through jpa-hibernate, lazy join, but I got an error.
Here my stuff:
Table user_role
user_role_id username ROLE
2 petroff ROLE_ADMIN
Table user
id username password salt email profile phone repassword
6 petroff 12345 ${config.salt} petroff#еуые.com test petroff prifile "" 12345
Part class User
#Entity
#Table(name = "tbl_user")
#Repassword(pass = "password", repass = "repassword")
public class User {
#Id
#Column(name = "id")
private int id;
#NotNull
#Size(min = 2, max = 64)
#Column(name = "username")
private String username;
#NotNull
#Size(min = 2, max = 64)
#Column(name = "password")
private String password;
#Column(name = "salt")
private String salt;
#Email
#Column(name = "email")
private String email;
#NotEmpty
#Column(name = "profile")
private String profile;
private String repassword;
#OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
private Set<UserRole> userRole = new HashSet<UserRole>();
public Set<UserRole> getUserRole() {
return userRole;
}
public void setUserRole(Set<UserRole> userRole) {
this.userRole = userRole;
}
Part class UserRole
#Entity
#Table(name = "user_roles")
public class UserRole {
#Id
#Column(name = "user_role_id")
private Integer userRoleId;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "username", nullable = false)
private com.blog.blog.entity.User user;
#Column(name = "role", nullable = false, length = 45)
private String role;
//getter and setter methods
public Integer getUserRoleId() {
return userRoleId;
}
public void setUserRoleId(Integer userRoleId) {
this.userRoleId = userRoleId;
}
call method
#Autowired
UserService us;
#RequestMapping(value = "/test", method = RequestMethod.GET)
public String printHello(ModelMap model, HttpSession session) {
com.blog.blog.entity.User u = us.getByUserName("petroff");
Set<UserRole> userRoles = u.getUserRole();
return "hello";
}
Service
#Service
#Transactional(readOnly = true)
public class UserServiceImpl implements UserService {
#Autowired
private UserRepository userRepository;
#PersistenceContext
private EntityManager entityManager;
#Override
public User addUser(User u) {
User savedUser = userRepository.saveAndFlush(u);
return savedUser;
}
#Override
public void delete(Integer id) {
userRepository.delete(id);
}
#Override
public User editUser(User u) {
return userRepository.saveAndFlush(u);
}
#Override
public List<User> getAll() {
return userRepository.findAll();
}
#Override
public User getByUserName(String name) {
return userRepository.findByUserName(name);
}
}
config file
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/config.properties"/>
<context:component-scan base-package="com.blog.blog.service.impl"/>
<jpa:repositories base-package="com.blog.blog.repositories"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.drivers}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
<property name="database" value="MYSQL"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<!-- spring based scanning for entity classes-->
<property name="packagesToScan" value="com.blog.blog.entity"/>
</bean>
<tx:annotation-driven/>
<bean id="userDetailsService"
class="com.blog.blog.service.impl.UserDetailsImpl">
</bean>
<import resource="spring-security.xml"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"/>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<bean id="sessionFactory" class="org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<aop:config>
<aop:pointcut id="userServicePointCut"
expression="execution(* com.blog.blog.service.impl.*Service.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="userServicePointCut"/>
</aop:config>
</beans>
I get User obj
com.blog.blog.entity.User u = us.getByUserName("petroff");
but when i call
Set<UserRole> userRoles = u.getUserRole();
i got an error:
Exception occurred in target VM: failed to lazily initialize a collection of role: com.blog.blog.entity.User.userRole, could not initialize proxy - no Session
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.blog.blog.entity.User.userRole, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:566)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:186)
at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:137)
at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:156)
at com.blog.blog.controller.Main.printHello(Main.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at
You will need to initialize lazy collection in the service if you want to use it on the caller side. Try this
#Override
public User getByUserName(String name) {
User u = userRepository.findByUserName(name);
Hibernate.initialize(u.getUserRole());
return u;
}
There is plenty of discussions on this topic online, look it up if you want more background info on the subject.
I write Spring web-app using Hibernate Search 4.5.1. When I try search it returns emty list. I think that problem in indexing. Dir for indexes was created, but after entity saving files in dir are not changing.
This is my spring config file
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<context:component-scan base-package="project"/>
<context:annotation-config/>
<jpa:repositories base-package="project" transaction-manager-ref="hibernateTransactionManager"
entity-manager-factory-ref="entityManagerFactory"/>
<bean id="entityManagerFactory" name="managerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="project"/>
<property name="dataSource" ref="postgresDataSource"/>
<property name="validationMode" value="NONE"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL81Dialect</prop>
</props>
</property>
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider"/>
</property>
</bean>
<bean id="postgresDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/LabourExchange"/>
<property name="username" value="postgres"/>
<property name="password" value="123456"/>
</bean>
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="dataSource" ref="postgresDataSource"/>
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="postgresDataSource"/>
<property name="packagesToScan" value="project"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.postgres.dialect}</prop>
<prop key="hibernate.max_fetch_depth">2</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">8</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.search.default.directory_provider">filesystem</prop>
<prop key="hibernate.search.default.indexBase">/var/lucene/indexes</prop>
</props>
</property>
</bean>
<!--security config-->
<security:http auto-config="true" use-expressions="true">
<security:intercept-url pattern="/pages/**" access="isAuthenticated()"/>
<security:form-login
login-page="/auth/login.jsf"
default-target-url="/pages/home.jsf"
authentication-failure-url="/auth/login.jsf?status=error"/>
<security:logout logout-success-url="/hello.jsf"/>
</security:http>
<security:authentication-manager>
<security:authentication-provider user-service-ref="UserDetailsService">
</security:authentication-provider>
</security:authentication-manager>
</beans>
My entity file
#Entity
#Table(name = "resumes")
#Indexed
public class Resume {
#Id #Column(name = "id") #GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
#Column(name = "title")
#Field(index= Index.YES, analyze= Analyze.YES, store= Store.NO)
#NotBlank #Size(min = 10, max = 100)
private String title;
#Column(name = "text")
#Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
#NotBlank #Size(min = 50, max = 10000)
private String text;
#Column(name = "salary")
#NotNull
private double salary;
#ManyToOne(optional = false)
#JoinColumn(name = "creator_id")
private User creator;
public User getCreator() {
return creator;
}
public void setCreator(User creator) {
this.creator = creator;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
I solved the problem by moving
<prop key="hibernate.search.default.directory_provider">filesystem</prop>
<prop key="hibernate.search.default.indexBase">/var/lucene/indexes</prop>
to jpaProperties tag
Here is my 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>
Here is my Disease.java:
#Entity
#Table(name="Disease")
public class Disease {
#Id
#GeneratedValue
#Column(name="DiseaseID")
public int DiseaseID;
#Column(name="Name")
public String Name;
}
Here is my another class:
#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;
}
I am new to hibernate and spring mvc framework and I just follows the guide in the tutorials.
I can query the result by using:
#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;
}
However, when I join two table by DiseaseID, it gives following error.
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]
I don't got the error when querying the result with one table only.
Here is the code that I used to join that two table:
#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;
}
What's wrong?
Thanks for help.
Supplementary:
Updated 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;
}
Updated 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>();
}
I used above entity and there is exception in deploying.
Do I need to add anything else?
I think you should not use d.ChrPosDisease instead use just ChrPosDisease as d is an alias for only Disease entity.
and it should work.
let me know if it doesn't work.
You should have mapping entry as:
#OneToMany(fetch = FetchType.LAZY, mappedBy = "disease")
private Set<ChrPosDisease> chrPosDisease = new HashSet<ChrPosDisease>();
mappedBy attribute notifies that the field is mapped by that particular entity property. So, this property should be the one on which connects entity on ManyToOne side.
Here Disease has many ChrPosDisease. And ChrPosDisease has one Disease. So mapped by column should be the one by which OneToMany field is bound with property on ManyToOne side.