I am using spring 3.2.2. I am facing issue while autowring the collection by spring's byType autowiring mode. I have created the below example.
Bean defination:
package com.springaction.testmultidimesionalcollection;
import java.util.ArrayList;
/**
* #author jinesh
*
*/
public class Address {
ArrayList<Country> country;
public ArrayList<Country> getCountry() {
return country;
}
/**
* #param country the country to set
*/
public void setCountry(ArrayList<Country> country) {
this.country = country;
}
}
Below is the spring configuration file testmultidimensionalcollection.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<bean id="addressMiddleEast" class="com.springaction.testmultidimesionalcollection.Address" autowire="byType">
</bean>
<bean id="countryChina" class="com.springaction.testmultidimesionalcollection.Country">
<property name="countryName" value="China" />
</bean>
<bean id="countryIndia" class="com.springaction.testmultidimesionalcollection.Country">
<property name="countryName" value="India" />
</bean>
<bean id="countryAus" class="com.springaction.testmultidimesionalcollection.Country">
<property name="countryName" value="Australia" />
</bean>
<bean id="middeastcountryQuatar" class="com.springaction.testmultidimesionalcollection.Country">
<property name="countryName" value="Quatar" />
</bean>
<bean id="middeastcountryIsrael" class="com.springaction.testmultidimesionalcollection.Country">
<property name="countryName" value="Israel" />
</bean>
<bean id="middeastcountryYemen" class="com.springaction.testmultidimesionalcollection.Country">
<property name="countryName" value="Yemen" />
</bean>
</beans>
Here I have strong type collection in the Address class. So all the elements of the country are supposed to be added inside the Country properties arraylist but when I am executing the below code I am getting the Null pointer exception.
package com.springaction.testmultidimesionalcollection;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* #author jinesh
*
*/
public class TestMultiDimensionalMain {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("com/springaction/testmultidimesionalcollection/testmultidimensionalcollection.xml");
Address addrs=(Address)context.getBean("addressMiddleEast");
System.out.println("address size:" + addrs.getCountry().size());
}
}
I am not able to understand, why spring is not able to automatically detect the country beans and add them to the array list property of address bean? Have I missed something in the configuration?
You haven't actually requested that anything be autowired. This bean
<bean id="addressMiddleEast" class="com.springaction.testmultidimesionalcollection.Address" autowire="byType">
</bean>
and class
public class Address {
ArrayList<Country> country;
public ArrayList<Country> getCountry() {
return country;
}
/**
* #param country the country to set
*/
public void setCountry(ArrayList<Country> country) {
this.country = country;
}
}
simply has property setters/getters. You need to use #Autowired (or the related annotations).
public class Address {
#Autowired
ArrayList<Country> country;
If you can't use #Autowired (why not???), you would need to create a bean of type List that has a reference to each of your Country beans, like in (now deleted) tichodrama's answer.
This is explained in the Spring IoC documentation here
Related
I am trying to understand approaches to access a database in a Multi threaded environment.
I implemented a following spring example that queries object from database in multiple threads, it works fine but I am not to sure if this is the right way to do it.
database.xml file looks like this
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#localhost:1522/orcl.168.0.106" />
<property name="username" value="system" />
<property name="password" value="admin" />
</bean>
<bean id="oracleJDBCTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name = "dataSource" ref = "dataSource" />
</bean>
</beans>
UserDetails object
package com.dataReader;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
public class UserDetails implements RowMapper {
private String user_name;
private String password;
public String getUser_name() {
return user_name;
}
public void setUser_name(String username) {
this.user_name = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
UserDetails userDetails = new UserDetails();
userDetails.setUser_name(rs.getString("USER_NAME"));
userDetails.setPassword(rs.getString("PASSWORD"));
return userDetails;
}
}
Application class is as follows
package com.dataReader;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
public class Application {
static JdbcTemplate oracleJDBCTemplate;
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("database.xml");
oracleJDBCTemplate = (JdbcTemplate) context.getBean("oracleJDBCTemplate");
List<String> userList = new ArrayList<String>();
userList.add("system");
userList.add("user-1");
userList.add("user-2");
userList.add("user-10");
userList.add("user-12");
for(int i=0;i<userList.size();i++) {
RunnerData runnerData = new RunnerData(oracleJDBCTemplate,userList.get(i));
Thread thread = new Thread(runnerData);
thread.start();
}
}
}
class RunnerData implements Runnable{
private JdbcTemplate oracleJDBCTemplate;
private String username;
RunnerData(JdbcTemplate jdbcTemplate,String username){
this.oracleJDBCTemplate=jdbcTemplate;
this.username=username;
}
#Override
public void run() {
UserDetails userDetails= oracleJDBCTemplate.queryForObject("select user_name,password from User_details where user_name=?",
new Object[] {username},BeanPropertyRowMapper.newInstance(UserDetails.class));
System.out.println(userDetails.getUser_name()+" "+userDetails.getPassword());
}
}
Is that a right approach to access database in a multi-threaded environment especially where we have large number of threads accessing database?
Wouldn't JdbcTemplate lock the database while querying the object. What are your experience in designing & implementing such a scenario?
Thanks for your input on this.
You would use database connection pooling.
Using Oracle connection pooling from OJDBC library is one way, for example:
<bean id="datasource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
<property name="connectionCachingEnabled" value="true" />
<property name="url" value="jdbc:oracle:thin:#localhost:1522/orcl.168.0.106" />
<property name="username" value="system" />
<property name="password" value="admin" />
<property name="connectionCacheProperties">
<props merge="default">
<prop key="MinLimit>5</prop>
<prop key="MaxLimit">100</prop>
</props>
</property>
</bean>
i am facing below error:
Type Exception Report
Message Request processing failed; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: customer is not mapped [from customer]
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: customer is not mapped [from customer]
Entity Class:
package com.luv2code.springdemo.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
//#Table(schema = "web_customer_tracker", name = "customer")
#Table(name="customer")
#Entity
public class Customer {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="id")
private int id;
#Column(name="first_name")
private String firstName;
#Column(name="last_name")
private String lastName;
#Column(name="email")
private String email;
public Customer() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
#Override
public String toString() {
return "Customer [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
}
}
DAO impl:
package com.luv2code.springdemo.dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.luv2code.springdemo.entity.Customer;
#Repository
public class CustomerDAOImpl implements CustomerDAO {
#Autowired
private SessionFactory sessionFactory;
#Override
#Transactional
public List<Customer> getCustomers() {
//get the current hibernate session
Session currentSession = sessionFactory.getCurrentSession();
//List customers = new ArrayList<Customer>();
//create a query
Query<Customer> theQuery=currentSession.createQuery("from customer", Customer.class);
//currentSession.createQuery("from Customer", Customer.class);
//execute query and get result list
List<Customer> customers=theQuery.getResultList();
// return the results
/* Customer cus1=new Customer();
cus1.setEmail("a#gmail.com");
cus1.setFirstName("Abhishek");
cus1.setId(10);
cus1.setLastName("Kumar");
customers.add(cus1); */
return customers;
}
}
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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Add support for component scanning -->
<context:component-scan base-package="com.luv2code.springdemo" />
<!-- Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Step 1: Define Database DataSource / connection pool -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&serverTimezone=UTC" />
<property name="user" value="springstudent" />
<property name="password" value="springstudent" />
<!-- these are connection pool properties for C3P0 -->
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="30000" />
</bean>
<!-- Step 2: Setup Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan" value="com.luv2code.springdemo.entity" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Step 3: Setup Hibernate transaction manager -->
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- Step 4: Enable configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="myTransactionManager" />
</beans>
There is a difference between the entity Customer and the relational table customer.
When you specify #Table(name="customer"), you ask your JPA implementation to use customer as table name which it is probably doing (check in your database).
When you specify createQuery("from customer", Customer.class), you ask your JPA implementation to create a JPQL query and customer is not a known entity because that's Customer or com.luv2code.springdemo.entity.Customer the entity.
I am using Spring-ORM to add a Employee* object in Table called emp using HibernateTemplate.
But when I'm trying to execute the program I'm getting this error:
Error creating bean with name 'testBean': Unsatisfied dependency expressed through field 'dao'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException:E rror creating bean with name 'empDaoImpl': Unsatisfied dependency expressed through field 'ht'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ht' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is
org.hibernate.tool.schema.spi.SchemaManagementException: Unable to execute schema management to JDBC target [alter table emp add empno number(10,0) not null]
This is My java bean
#Service
public class TestBean
{
#Autowired
private EmpDao dao;
public void persistEmp(int empid,String empName,int sal,int deptno)
{
Employee e=new Employee();
e.setEmpid(empid);
e.setEmpName(empName);
e.setEmpSalary(sal);
e.setDeptNum(deptno);
dao.save(e);
}
public void updateEmp(int empid,String empName,int sal,int deptno)
{
Employee e=new Employee();
e.setEmpid(empid);
e.setEmpName(empName);
e.setEmpSalary(sal);
e.setDeptNum(deptno);
dao.update(e);
}
public void deleteEmp(int empid)
{
dao.deleteEmployee(empid);
}
public void selectEmps()
{
List lt=dao.selectEmployees();
Iterator it=lt.iterator();
while(it.hasNext())
{
Employee e1=(Employee)it.next();
System.out.println(e1);
}
}
}
*This is my Entity class
#Entity
#Table(name="emp")
public class Employee
{
#Id
#Column(name="empno")
private int empid;
#Column(name="ename")
private String empName;
#Column(name="sal")
private int empSalary;
#Column(name="deptno")
private int deptNum;
//Setters and Getters
public String toString()
{
return "Employee["+empid+" "+empName+" "+empSalary+" "+deptNum+"]";
}
}
This is my EmpDaoImpl class
#Repository
#Transactional
public class EmpDaoImpl implements EmpDao
{
#Autowired
private HibernateTemplate ht;
public void deleteEmployee(int empid)
{
Employee e=(Employee)ht.get(Employee.class, empid);
ht.delete(e);
System.out.println("one Employee object is deleted");
}
public List selectEmployees()
{
List empList=ht.find("from Employee e");
return empList;
}
public void save(Employee e)
{
ht.save(e);
System.out.println("Object is saved ");
}
public void update(Employee e)
{
ht.update(e);
System.out.println("Object is Updated");
}
}
This is my Main class
public class Main
{
public static void main(String[] args)
{
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
TestBean testB=(TestBean)ctx.getBean("testBean");
testB.persistEmp(7999, "naveen", 55555, 40);
System.out.println("===============================");
}
}
This is my applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.pack1"></context:component-scan>
<bean id="ht" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="ds"></property>
<property name="annotatedClasses">
<list>
<value>com.pack1.entity.Employee</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
</value>
</property>
</bean>
<bean id="txm" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:#localhost:1521:xe"/>
<property name="username" value="system"/>
<property name="password" value="tiger"/>
</bean>
<tx:annotation-driven transaction-manager="txm"/>
</beans>
When hibernate.hbm2ddl.auto is set to update Hibernate won't modify existing table column definitions. So, you can manually alter the column definition or drop the table/column and run your code. In the latter case, hibernate will create the column if it is not present.
Please refer here, here and here for more info on hibernate.hbm2ddl.auto options and how they work.
I am going to be confused. I am trying to get Hibernate work with Spring, I can not get rid of a BeanCreationException. Please help me. I am new to Spring (and also Hibernate).
The problem is caused in a controller, having a private attribute userService which is annotated with #Autowired.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.aerts.service.UserService com.aerts.controller.TestController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.aerts.service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
I am really confused, please help me somebody.
Here is my root-context.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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<tx:annotation-driven />
<context:component-scan base-package="com.aerts.controller">
</context:component-scan>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:application.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://....."/>
<property name="username" value="....."/>
<property name="password" value="....."/>
<property name="initialSize" value="5"/>
<property name="maxActive" value="20"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
Here is my User.java
package com.aerts.domain;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name="User")
public class User {
#Id
#GeneratedValue()
#Column(name="id")
int id;
#Column(name="gender")
private Gender gender;
#Column(name="birthdate")
private Date birthdate;
#Column(name="firstname")
private String firstname;
#Column(name="surname")
private String surname;
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public Date getBirthdate() {
return birthdate;
}
public void setBirthdate(Date age) {
this.birthdate = age;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname.trim();
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname.trim();
}
}
My UserDaoImpl:
package com.aerts.dao;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import com.aerts.domain.User;
#Service
public class UserDaoImpl implements UserDao{
#Autowired
private SessionFactory sessionFactory;
#Override
public void addUser(User user) {
sessionFactory.getCurrentSession().save(user);
}
#Override
public List<User> listUser() {
return sessionFactory.getCurrentSession().createQuery("from User")
.list();
}
#Override
public void removeUser(int id) {
User user = (User) sessionFactory.getCurrentSession().get(
User.class, id);
if (user != null) {
sessionFactory.getCurrentSession().delete(user);
}
}
#Override
public void updateUser(User user) {
sessionFactory.getCurrentSession().update(user);
}
}
And my UserServiceImpl:
package com.aerts.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.aerts.dao.UserDao;
import com.aerts.domain.User;
#Service
public class UserServiceImpl implements UserService {
#Autowired
private UserDao userDao;
#Override
#Transactional
public void addUser(User user) {
userDao.addUser(user);
}
#Override
#Transactional
public List<User> listUser() {
return userDao.listUser();
}
#Override
#Transactional
public void removeUser(int id) {
userDao.removeUser(id);
}
#Override
#Transactional
public void updateUser(User user) {
userDao.updateUser(user);
}
}
I would really appreciate it if somebody could help me, i am going to be desperate...
Add the service class package to the component-scan base-package list in the application context file
<context:component-scan
base-package="
com.aerts.controller
com.aerts.service">
</context:component-scan>
I'm not expertise in spring but I assume that you have a problem with your service, if you use an interface you should inject ut and not your class...
see this track...
Spring expected at least 1 bean which qualifies as autowire candidate for this dependency
Here are my tables:
ORDER
CREATE TABLE ORDER_DETAILS (
ID INT NOT NULL AUTO_INCREMENT
, ORDER_ID VARCHAR(60) NOT NULL
, ADDRESS_ID INT
-- , DRIVER_ID INT
, BARCODE_ID INT
, CUSTOMER_ID VARCHAR(60)
, CUSTOMER_PIN VARCHAR(60)
, UNIQUE UQ_ORDER_ID_1 (ORDER_ID)
, FOREIGN KEY(BARCODE_ID) REFERENCES public.BARCODE(ID)
, PRIMARY KEY (ID)
);
AND BARCODE
CREATE TABLE BARCODE (
ID INT NOT NULL AUTO_INCREMENT
, BARCODE_DETAILS VARCHAR(60) NOT NULL
, DRIVER_ID INT
, VERSION INT NOT NULL DEFAULT 0
, UNIQUE UQ_BARCODE_ID_1 (BARCODE_DETAILS)
, PRIMARY KEY (ID));
Order to Barcode is Many To One Relationship i.e. Many Order Can Have One barcode
Here Are My repositories
OrderRepository
package com.eppvd.application.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.eppvd.application.domain.Address;
import com.eppvd.application.domain.Customer;
import com.eppvd.application.domain.Driver;
import com.eppvd.application.domain.Order;
public interface OrderRepository extends JpaRepository<Order, Long> {
Order findByOrderId(String orderId);
// Order findByBarcode(String barcode);
Order findByCustomerPin(String customerPin);
// Order findByDriver(Driver driver);
Order findByAddress(Address address);
Order findByCustomer(Customer customer);
}
And BarcodeRepository
package com.eppvd.application.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.eppvd.application.domain.Barcode;
public interface BarcodeRepository extends JpaRepository<Barcode, Long> {
Barcode findByBarcode(String barcode);
}
Here Are My ServiceImpls
OrderServiceImpl
package com.eppvd.application.service.jpa;
/** imports **/
#Service("OrderService")
#Repository
#Transactional
public class OrderServiceImpl implements OrderService {
/** Other Methods **/
#Override
#Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public Order save(Order order) {
return orderRepository.save(order);
}
#Override
public void flush(){
orderRepository.flush();
}
}
And BarcodeServiceImpl
package com.eppvd.application.service.jpa;
#Service(value="barcodeService")
#Repository
#Transactional
public class BarcodeServiceImpl implements BarcodeService{
#Autowired
BarcodeRepository barcodeRepository;
#Override
#Transactional(propagation=Propagation.REQUIRED,readOnly=false)
public Barcode save(Barcode barcode) {
return barcodeRepository.save(barcode);
}
}
And Entities
package com.eppvd.application.domain;
#Entity
#Table(name = "order_details")
public class Order implements Serializable {
private Barcode barcode;
#ManyToOne(optional = false)
#JoinColumn(name="BARCODE_ID")
public Barcode getBarcode() {
return barcode;
}
public void setBarcode(Barcode barcode) {
this.barcode = barcode;
}
}
package com.eppvd.application.domain;
#Entity
#Table(name = "barcode")
public class Barcode implements Serializable {
private static final long serialVersionUID = 3202943305655732979L;
private Long id;
private int version;
private String barcode_details;
private Set<Order> orders ;
#Column(name="BARCODE_DETAILS")
public String getBarcodeDetails() {
return barcodeDetails;
}
public void setBarcodeDetails(String barcode) {
this.barcodeDetails = barcode;
}
#OneToMany(cascade=CascadeType.ALL,mappedBy="barcode",fetch=FetchType.EAGER)
public Set<Order> getOrders() {
return orders;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
}
and application Context
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:schema.sql"/>
<!-- <jdbc:script location="classpath:test-data.sql"/> -->
</jdbc:embedded-database>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<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.eppvd.application.domain"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</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>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<context:annotation-config/>
<jpa:repositories base-package="com.eppvd.application.repository"
entity-manager-factory-ref="emf"
transaction-manager-ref="transactionManager"/>
<!-- <bean id="customerService" class="com.eppvd.application.service.jpa.CustomerServiceImpl"> -->
<!-- </bean> -->
<context:component-scan base-package="com.eppvd.application.service.jpa"></context:component-scan>
</beans>
And JUNit Test
package com.eppvd.application.service;
import static org.junit.Assert.*;
#ContextConfiguration(locations="classpath:datasource-tx-jpa.xml")
#RunWith(SpringJUnit4ClassRunner.class)
#Transactional
public class OrderServiceTest {
#Autowired
private OrderService orderService;
#Autowired
private BarcodeService barcodeService;
#Before
public void createAddressCustomerDriver(){
Barcode barcode = new Barcode();
barcode.setBarcodeDetails("1234");
barcodeService.save(barcode);
}
#Test
public void testCreateOrder() {
Barcode barcode = barcodeService.findByBarcode("1234");
Order order = new Order();
order.setOrderId("OD001");
order.setBarcode(barcode);
order.setCustomerPin("1234");
order.setCustomer(customerService.findByFirstName("Abhijit"));
order.setAddress(addressService.findByStreet("BTM Layout"));
orderService.save(order);
orderService.flush();
Set<Order> orders = barcode.getOrders();
assertEquals("1234", orderService.findByOrderId("OD001").getBarcode().getBarcodeDetails()); /**Works **/
assertEquals(1, orders.size()); /**FAILS **/
}
}
I tried To be as detailed as possible. So it became a bit long. Any help would be greatly appreciated. I tried to figure out everything logically but not too sure what am I missing.There is little documentation around this area
You created an Order, and initialized its barcode. But you never added the order to the set of orders of the barcode. So its set of barcodes is empty. That's expected.
You're supposed to maintain the two sides of an association if you expect them to be coherent.