JPA - Entities are not stored in database - spring

I am facing a problem, when I tried to insert a data into Database through JPA (#persistanceContex)
Observations
Not getting any errors;
Record is not storing into database (save)
When I tried with listAll() ; it retrieving the data from database
Domain
#Entity
public class Test {
#Id
private int id;
#Column(name="full_name")
private String fullName;
#Column(name="mobile_number")
private int mobileNumber;
.....
}
DAO Class
#Repository("testDAO")
#Transactional
public class TestDAO {
private EntityManager entityManager;
#PersistenceContext(unitName="CRUD_Test_Annotation")
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
public void save(Test test){
entityManager.persist(test);
}
}
Service
#Service("testService")
#Transactional
public class TestService {
private static final Logger logger = LoggerFactory.getLogger(TestService.class);
#Autowired(required=true)
private TestDAO testDAO;
public void save(Test test){
logger.info("TestService::save()");
testDAO.save(test);
}
public void list(){
testDAO.getAll();
}
}
Controller
#RequestMapping(value = "/add", method = RequestMethod.GET)
public String add(Locale locale, Model model) {
Test test = new Test();
test.setId(xx);
test.setFullName("xxxxx");
test.setMobileNumber(yyyyyy);
testService.save(test);
return "home";
}
application-context.xml
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Declare a JPA entityManagerFactory-->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"></property>
<property name="persistenceUnitName" value="CRUD_Test_Annotation" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
</bean>
</property>
</bean>
<!-- Declare a transaction manager-->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

First of all, you don't need two transaction boundaries, I suggest you remove #Transactional from your DAO and keep the one in your service.
Start by verifying that spring-transaction has initiated a transaction: Use the debugger and stop the application after the transaction boundary, for instance in your TestService.save-method. If transactions are running, you will see org.springframework.transaction.interceptor.TransactionInterceptor#invoke in the call stack. If you don't see the TransactionInterceptor, then that's your problem. Post your persistence.xml file if transactions are running.

Related

Bean variable not loading during unit test

I am trying to test some dao code in Spring Boot. Here is my class under test:
#Repository
public class AreaDao
{
#Setter private JdbcTemplate jdbcTemplate;
#Setter private AreaMapper areaMapper;
#Getter #Setter private String sql;
public List getArea(String studioId)
{
String query = String.join(this.getSql(),studioId); // its throwing a NPE here
List areaList = jdbcTemplate.query(query, areaMapper);
return areaList;
}
}
The variable sql is loading from the bean xml:
<bean id="AreaDao" class="com.studio.dao.AreaDao">
<property name="sql" value="SELECT DISTINCT(rGrp.GRP_N) AS AreaName
FROM DB.S_GRP rGrp
WHERE rGrp.STR_I = " />
<property name="jdbcTemplate" ref="JdbcTemplate" />
<property name="areaMapper" ref="AreaMapper" />
</bean>
<bean id="AreaMapper" class="com.studio.mapper.AreaMapper" />
<bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="DMDataSource" />
</bean>
And here is my test class:
#RunWith(SpringRunner.class)
#SpringBootTest
public class AreaDaoTest {
#Mock
JdbcTemplate jdbcTemplateMock;
#Mock
AreaMapper areaMapper;
#InjectMocks
#Spy
AreaDao areaDao;
private List areas;
#Before
public void setup(){
areas = new ArrayList();
}
#Test
public void testGetArea() throws SQLException {
String studioId = "12345";
when(jdbcTemplateMock.query(any(String.class), any(RowMapper.class))).thenReturn(areas);
List areas = areaDao.getArea(studioId);
Assert.assertNotNull(areas);
Assert.assertTrue(areaDao.getSql().contains(studioId));
}
}
Whenever I try to run the test its throwing a NPE in loading the sql var from the bean xml. I tried adding #ContextConfiguration or just #Autowired on the dao in the test but it didn't work. Also how can I verify in the test if its loading a value?
Appreciate your help.

Spring :: #Transactional not working

I am new to Spring and learning the transaction concepts. Unable to get the #Transactional to work.
Use Case:
Data insert of Employee and Employee details should rollback when getEmployee() throws RunTimeException. But the rollback is not happening.
I am using Oracle database 11g and spring 4.3.1.RELEASE. Below is the standalone java code am running.
Code
public static void main( String[] args )
{
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("spring-bean.xml");
ctx.registerShutdownHook();
Employee emp = new Employee("149518", "Mickey", "Mouse", "15 years", "tennis");
IEmployee empIntfc = (IEmployee)ctx.getBean("empService");
try {
empIntfc.createEmployee(emp);
empIntfc.createEmployeeDetails(emp);
//Below will throw RunTime Exception
empIntfc.getEmployee(2);
}catch (Exception e ) {
e.printStackTrace();
} finally {
ctx.close();
}
}
EmployeeService.java
public class EmployeeService implements IEmployee {
private DataSource dataSource;
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
jdbcTemplate = new JdbcTemplate(this.dataSource);
}
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
#Override
#Transactional
public int createEmployee(Employee emp) {
String sql1 = "INSERT INTO TEST_T1(EMP_ID, EMP_FNAME, EMP_LNAME) values
(?,?,?)";
return getJdbcTemplate().update(sql1, emp.getEmpId(),
emp.getEmpFirstName(), emp.getEmpLastName());
}
#Override
#Transactional
public int createEmployeeDetails(Employee emp) {
String sql = "INSERT INTO TEST_T2(EMP_ID, EXP, SKILLS) values (?,?,?)";
return getJdbcTemplate().update(sql, emp.getEmpId(), emp.getExp(),
emp.getSkills());
}
#Override
#Transactional(readOnly = true, noRollbackFor=RuntimeException.class)
public Employee getEmployee(int empId) {
throw new RuntimeException("Intentional runtime exception");
}
}
spring-bean.xml
<beans xmlns="http://www.springframework.org/schema/beans">
<context:annotation-config/>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:#//xxxx:1521/xxxx"/>
<property name="username" value="user"/>
<property name="password" value="user"/>
</bean>
<bean id="empService" class="com.service.EmployeeService">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
Your main method is not transactional ... means : entering 'createEmployee' creates a new transaction and commits it, 'createEmployeeDetails' creates a new transaction and commits it .

Why is my #Autowired field null here?

This is not a duplicate of this question. So please don't close it for "is duplicate of" reasons..
I am trying to autowire a private field in my service class using this tutorial. My problem is that restaurantOwnerRepository remains null and does not get initialized.
servlet-context.xml
<context:component-scan base-package="com.mahlzeit.web.server" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="restaurantOwnerRepository" class="com.mahlzeit.web.server.dao.RestaurantOwnerRepository">
<constructor-arg>
<ref bean="sessionFactory" />
</constructor-arg>
</bean>
Service code:
#Component
public class RestaurantInformationServiceImpl extends XsrfProtectedServiceServlet implements RestaurantInformationService {
private static final long serialVersionUID = -4088840947018614411L;
#Autowired
private RestaurantOwnerRepository restaurantOwnerRepository;
private final static Logger logger = Logger.getLogger(RestaurantInformationServiceImpl.class);
#Override
public List<RestaurantDTO> getAvailableRestaurants() {
// restaurantOwnerRepository is 'null'
List<Restaurant> availableRestaurants = restaurantOwnerRepository.getAvailableRestaurants(getSessionId());
return null;
}
private String getSessionId() {
HttpServletRequest httpRequest = getThreadLocalRequest();
return httpRequest.getSession().getId();
}
}
RestaurantOwnerRepository.java
public class RestaurantOwnerRepository implements RestauranOwnerDAO {
private SessionFactory sessionFactory;
public RestaurantOwnerRepository(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
// ..
}
What could be the reason for this?
here is a sample controller for your example , you should define the bean in your context.xml or if you place it in this package : com.mahlzeit.web.server it will be managed by spring automatically , cause as i see you have placed the context:component-scan
#Controller
public class RestaurantInformationServiceImpl {
#Autowired
private RestaurantOwnerRepository restaurantOwnerRepository;
#RequestMapping(value="/")
public #ResponseBody ModelAndView getRestaurants(
HttpServletRequest request,
HttpServletResponse response) {
ModelAndView model = new ModelAndView("yourPage");
List<?> rests = restaurantOwnerRepository.getAvailableRestaurants(httpRequest.getSession().getId());
model.addObject("restList", rests );
return model;
}
}

Annotation #Transactional does not working

I am trying to use the annotation #Transactional to access my MySQL using Hibernate, Spring and JSF. My problem is:
When I use the annotation #Transactional at my managedBean to make a query I got this error:
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:65)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:687)
at br.com.rpg.DAO.AbstractDAO.getCurrentSession(AbstractDAO.java:14)
at br.com.rpg.DAO.CountryDAO.findAll(CountryDAO.java:14)
at br.com.rpg.managedBeans.SignupBean.init(SignupBean.java:45)
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)
I don't know what I'm doing wrong. My code and xml config is:
application-config.xml
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="br.com.rpg.DO" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
<context:component-scan base-package="br.com.rpg" />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
My MB
#Named
#Scope("request")
public class SignupBean implements Serializable {
private static final long serialVersionUID = 1787096549063029840L;
#Inject
private CountryDAO country;
#Inject
private UserDAO user;
private Map<String, Integer> countries;
private Integer selected;
private String username;
private String password;
#PostConstruct
#Transactional
public void init() {
List<CountryDO> findAll = country.findAll();
countries = new HashMap<String, Integer>();
for (CountryDO countryDO : findAll) {
countries.put(countryDO.getName(), countryDO.getId());
}
}
public Map<String, Integer> getCountries() {
return countries;
}
public void setCountries(Map<String, Integer> countries) {
this.countries = countries;
}
public Integer getSelected() {
return selected;
}
public void setSelected(Integer selected) {
this.selected = selected;
}
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;
}
My Country DAO has the #Named annotation. Someone could help me?
Thx.
you have applied #PostConstruct and #Transactional on init method. init method would be called before applying any AOP Proxy interceptor is applied because of #PostConstruct. Hence at the time of invoking init there is no transaction proxy applied. if you need to call init method on Application startup use ApplicationEvent

Getting the entity manager

i'm using spring data jpa but i want execute some costume query so how can i get the entity manager in my java classes to make entityManager.createQuery(..)
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="persistenceYous" />
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="persistence" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
</bean>
You can obtain your EntityManager as in any other spring application:
public class ProductDaoImpl implements ProductDao {
private EntityManager em;
#PersistenceContext
public void setEntityManager(EntityManager em) {
this.em = em;
}
public Collection loadProductsByCategory(String category) {
em. ....
....
}
}
See: http://docs.spring.io/spring/docs/4.0.0.RELEASE/spring-framework-reference/html/orm.html#orm-jpa-straight
In your applicationContext.xml, check that you have the following:
<context:annotation-config />
That will add support for several annotations, such as #PersistenceContext, that injects an EntityManager. So in your Spring-managed beans, you can do:
public class MyClass {
private EntityManager entityManager;
#PersistenceContext
public void setEntityManager(EntityManager em) {
this.entityManager = em
}
public void myMethod() {
Query q = entityManager.createQuery(...);
// ...
}
}
If you want to add suport for just #PersistenceContext, and not the other annotations that <context:annotation-config /> supports, you would delete that from the applicationContext.xml and add the specific BeanPostProcessor:
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" scope="singleton">
<!-- Optional configuration of PersistenceAnnotationBeanPostProcessor, for advanced use cases -->
</bean>
Firstly follow this link to get the steps required to implement..
check this link
In second step he uses #Autowired private JdbcOperations operations;
But that doesn't work for me, so i used the following code.. Which worked for me.
#Component
public class MayorRepositoryImpl extends JdbcDaoSupport implements MayorRepositoryCustom {
#Override
public List<Employee> getUsers(String role) {
return getJdbcTemplate().query("Write your custom query",new RowMapper<Employee>(){
#Override
public Employee mapRow(ResultSet rs, int rownumber) throws SQLException {
// Mapping each row and adding to list and returning the list
Employee employeeBean=new Employee();
employeeBean.setId(rs.getInt("id"));
employeeBean.setEmployeeNumber(rs.getString("employeeNumber"));
employeeBean.setName(rs.getString("name"));
employeeBean.setWorkEmailAddress(rs.getString("workEmailAddress"));
employeeBean.setPersonalEmailAddress(rs.getString("personalEmailAddress"));
return employeeBean;
}
});
}
}
This is perfectly working for me..
If you find any difficulty let me know to help you.
Cheers

Resources