vendredi 27 février 2015

Migration from ObjectDB to MySQL in Spring

I've been using objectDB as a database in my Spring application, yet it is only free for up to 10 tables, so i've tried to migrate to MySQL. When I look into database the tables are created, but when I try to persist I get the NullPointerException. Here are my configuration files:


persistence.xml:



<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://ift.tt/UICAJV" xmlns:xsi="http://ift.tt/ra1lAU" xsi:schemaLocation="http://ift.tt/UICAJV http://ift.tt/O9YdEP">
<persistence-unit name="cinemabookPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>cinemaDbCon</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>


spring-servlet.xml:



<?xml version="1.0" encoding="windows-1252"?>
<beans xmlns="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU"
xmlns:p="http://ift.tt/1jdM0fE"
xmlns:context="http://ift.tt/GArMu7"
xmlns:mvc="http://ift.tt/1bHqwjR"
xmlns:aop="http://ift.tt/OpNdV1"
xmlns:tx="http://ift.tt/OGfeU2"
xsi:schemaLocation="
http://ift.tt/GArMu6
http://ift.tt/QEDs1e
http://ift.tt/GArMu7
http://ift.tt/QEDs1k
http://ift.tt/1bHqwjR
http://ift.tt/1bVJL9q
http://ift.tt/OpNdV1
http://ift.tt/QEDs1g
http://ift.tt/OGfeU2
http://ift.tt/1cQrvTl">

<!-- Use @Component annotations for bean definitions -->

<context:component-scan base-package="cinema"/>

<!-- Use @Controller annotations for MVC controller definitions -->

<mvc:resources mapping="/resources/**" location="/resources/" />

<mvc:annotation-driven />

<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.glassfish.GlassFishLoadTimeWeaver"/>
</property>
</bean>

<tx:jta-transaction-manager />

<tx:annotation-driven />

<bean class=
"org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
</bean>

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<import resource="/spring-security.xml" />
</beans>


Model City:



package cinema.model;


import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;


@Entity
public class City implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;

protected City() {

}

public City(Long id, String name) {
this.id = id;
this.name = name;
}

public City(String name) {
this.name = name;
}


public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


@Override
public String toString() {
return name;
}
}


CityService.java:



package cinema.services;

import cinema.model.City;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;


public interface CityService {

// Stores a new city:
@Transactional
public void persist(City city);

// Retrieves all the citys:
public List<City> getAllCities();

public City getCityById(Long id);

public void deleteAllCities();
}


CityServiceImpl.java:



package cinema.services.impl;


import cinema.services.CityService;
import cinema.model.City;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Component
public class CityServiceImpl implements CityService {
// Injected database connection:
@PersistenceContext(unitName="cinemabookPU")
private EntityManager em;

public EntityManager getEm() {
return em;
}

public void setEm(EntityManager em) {
this.em = em;
}


// Stores a new city:
@Transactional
public void persist(City city) {
em = getEm();
System.out.println("EM "+em);
em.persist(city);
}

// Retrieves all the citys:
public List<City> getAllCities() {
TypedQuery<City> query = em.createQuery(
"SELECT c FROM City c", City.class);
return query.getResultList();
}

public City getCityById(Long id) {
TypedQuery<City> query = em.createQuery(
"SELECT c FROM City c WHERE c.id = :id", City.class);
return query.setParameter("id", id).getSingleResult();
}

@Transactional(propagation = Propagation.REQUIRED)
public void deleteAllCities() {
TypedQuery<City> query = em.createQuery(
"DELETE FROM City", City.class);
query.executeUpdate();
}
}


Any help will be greatly appreciated.


Aucun commentaire:

Enregistrer un commentaire