Hello everyone,
I'm developing a little POC using hibernate, H2 database and Spring.
My problem is i expect the automatic database generation, but it isn't happening. Any ideas? Below you can see my configurations:
applicationContext.xml
persistence.xmlCode:<?xml version="1.0" encoding="UTF-8" standalone="no"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 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-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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="hellospring.beans"/> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.h2.Driver" /> <property name="url" value="jdbc:h2:~/agenda" /> <property name="username" value="sa" /> <property name="password" value="sa" /> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="generateDdl" value="true"/> <property name="showSql" value="true" /> </bean> </property> <property name="jpaProperties"> <props> <prop key="hibernate.hbm2ddl.auto">create-drop</prop> </props> </property> </bean> </beans>
Main.javaCode:<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="hello-spring-pu" transaction-type="RESOURCE_LOCAL"> <class>hellospring.entity.Contato</class> </persistence-unit> </persistence>
Contato.javaCode:package hellospring; import hellospring.beans.Agenda; import hellospring.beans.SampleBean2; import hellospring.entity.Contato; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx = // new ClassPathXmlApplicationContext("applicationContext.xml"); Agenda agenda = ctx.getBean(Agenda.class); System.out.println(agenda.listar()); Contato c = new Contato(); c.setNome("Joe"); c.setEndereco("Street one"); c.setTelefone("12345678"); agenda.salvar(c); } }
Agenda.javaCode:package hellospring.entity; import javax.persistence.Column; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Table public class Contato { @Id @GeneratedValue private int id; @Column private String nome; @Column private String endereco; @Column private String telefone; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getNome() { return nome; } public void setNome(String nome) { this.nome = nome; } public String getEndereco() { return endereco; } public void setEndereco(String endereco) { this.endereco = endereco; } public String getTelefone() { return telefone; } public void setTelefone(String telefone) { this.telefone = telefone; } }
And this is what happens when i execute my sample:Code:package hellospring.beans; import hellospring.entity.Contato; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.stereotype.Component; @Component public class Agenda { @PersistenceContext private EntityManager em; public void salvar(Contato c) { em.persist(c); } public void excluir(int id) { em.createQuery("delete from Contato c where c.id = :id")// .setParameter("id", id).executeUpdate(); } public List<Contato> listar() { return em.createQuery("select c from Contato c", Contato.class) .getResultList(); } }
http://pastebin.com/tWmnZz4b
AFAIK the "<property name="generateDdl" value="true"/>" should do the job for me, yet the stacktrace acts as if i wan't mapped Contato.
Any help is welcome.


Reply With Quote
