Announcement Announcement Module
Collapse
No announcement yet.
The database isn't being created Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • The database isn't being created

    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
    Code:
    <?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>
    persistence.xml
    Code:
    <?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>
    Main.java
    Code:
    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);
    	}
    
    }
    Contato.java
    Code:
    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;
    	}
    
    }
    Agenda.java
    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();
    	}
    }
    And this is what happens when i execute my sample:
    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.

  • #2
    Hello everyone,

    tried change the database to mysql but got the same error. Any ideas?

    Comment


    • #3
      Hello everyone,

      It's solved now.

      The problem was the @Table annotation. I should use @Entity instead.

      Comment

      Working...
      X