I'm trying to set up a sample project using Spring Data JPA with Hsqldb
I made the person class that would be model
, the interface
repository and a main class to run.
I did not create the database because I have a question if Spring created for me automatically as Hibernate has this option.
Class person
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 Eclipse
window to select a class from a list of some Hsqldb
classes. So I'm confused about what it can be.
My xml configuration file for 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>