Problem configuring spring data jpa project with Hsqldb

Asked

Viewed 429 times

2

I am trying to set up a project example using Spring Data JPA with Hsqldb

I made the class I would be model, to interface repository and a main class to run.

I didn’t create the bank because I have a doubt if Spring created it for me automatically as Hibernate has this option.

Personal class

package data.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import org.springframework.data.jpa.domain.AbstractPersistable;

@Entity
@Table(name = "pessoa")
public class Pessoa extends AbstractPersistable<Long> {

    @Column(name = "nome", nullable = false, length = 25, unique = false)
    private String nome;

    @Column(name = "sobrenome", nullable = false, length = 25, unique = false)
    private String sobrenome;

    @Column(name = "idade", nullable = false, unique = false)
    private Integer idade;

    public String getNome() {
        return nome;
    }

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

    public String getSobrenome() {
        return sobrenome;
    }

    public void setSobrenome(String sobrenome) {
        this.sobrenome = sobrenome;
    }

    public Integer getIdade() {
        return idade;
    }

    public void setIdade(Integer idade) {
        this.idade = idade;
    }

}

Repository class

package data.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import data.model.Pessoa;

public interface PessoaRepository extends JpaRepository<Pessoa, Long> {

}

Test Class

package data.teste;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import data.repository.PessoaRepository;

@Component
public class Test {

    @Autowired
    private PessoaRepository pessoaRepository;

    public PessoaRepository getPessoaRepository() {
        return pessoaRepository;
    }

    public void setPessoaRepository(PessoaRepository pessoaRepository) {
        this.pessoaRepository = pessoaRepository;
    }

}

When running main, it opens a window of the Eclipse to select a class within a list of some classes of the Hsqldb. So I’m confused about what might be.

My xml configuration file Spring.

Xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
                        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
                        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd">

    <context:annotation-config/>

    <jpa:repositories base-package="data.repository" query-lookup-strategy="create-if-not-found"/>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true"/>
                <property name="generteDdl" value="true"/>
            </bean>
        </property>
        <property name="persistenceUnitName" value="persistenceUnitName"/>
    </bean>

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

</beans>

1 answer

1

Hello!

This may occur because the main method signature is incorrect or because the class containing the main method is not defined in Class-Path.

  • Signature of the correct main method:

     public static void main(String[] args) { ... }
    
  • You can run the main method by pressing with the right click on top of the method, and inside the menu Run As and click on Java Application.

  • It is also possible create a configuration through the menu of the Eclipse Run >> Run Configurations...

Note: It will be necessary load the Spring context inside the main to be able to access the Beans.

Abs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.