jeudi 16 avril 2015

Entity Manager NULL - Spring MVC JPA

My Object Model



@Entity
@Table(name = "jlocalidades", catalog = "7jogos")
public class Jlocalidades implements java.io.Serializable {

private Integer id;
private String nome;
private String descricao;

public Jlocalidades() {
}

public Jlocalidades(String nome, String descricao) {
this.nome = nome;
this.descricao = descricao;
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "Id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}

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

@Column(name = "Nome", nullable = false, length = 200)
public String getNome() {
return this.nome;
}

public void setNome(String nome) {
this.nome = nome;
}

@Column(name = "Descricao", nullable = false, length = 200)
public String getDescricao() {
return this.descricao;
}

public void setDescricao(String descricao) {
this.descricao = descricao;
}

}


My Servlet



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU"
xmlns:context="http://ift.tt/GArMu7"
xmlns:jee="http://ift.tt/OpNaZ5"
xmlns:tx="http://ift.tt/OGfeU2"
xsi:schemaLocation="http://ift.tt/GArMu6 http://ift.tt/GAf8ZW
http://ift.tt/1bHqwjR http://ift.tt/1fmimld
http://ift.tt/GArMu7 http://ift.tt/1bb5cbf
http://ift.tt/OpNaZ5 http://ift.tt/1AWhlbL
http://ift.tt/OGfeU2 http://ift.tt/OGffan
">


<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->

<context:component-scan base-package="com.dtr.oas" />
<context:annotation-config/>



<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<!-- <resources mapping="/resources/**" location="/resources/" /> -->

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>



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

<bean id="mysqlDS"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://192.168.254.38:3306/7jogos" />
<property name="username" value="root" />
<property name="password" value="6+1Log.pt" />
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="mysqlDS"/>
<property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence" />
</bean>

<tx:annotation-driven/>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

</beans>


My controller that gives the ERROR



@Autowired
private SessionFactory sessionFactory;

private Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}


@PersistenceContext
private EntityManager entityManager;

@Transactional
public List<Jlocalidades> All (){
log.debug("getting all Jlocalidades");
try {

List<Jlocalidades> instance = entityManager.createQuery("SELECT * FROM jlocalidades").getResultList();
log.debug("get successful");
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}


In this code the entityManager is null, what i'm doing wrong ? what is missing ?


My Persistence



<?xml version="1.0" encoding="UTF-8" ?>


http://ift.tt/O9YdEP" version="2.0" xmlns="http://ift.tt/UICAJV"> com.dtr.oas.model.Jlocalidades false



<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://192.168.254.38:3306/7jogos" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="6+1Log.pt" />

<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="eclipselink.logging.level" value="SEVERE"/>
</properties>

</persistence-unit>

Aucun commentaire:

Enregistrer un commentaire