2016-04-15 76 views
0

我目前尝试为Spring + JPA + Hibernate创建一个Spring最小的例子。至少有一个JPA元模型必须存在!但它产生了?

我创建了以下类:

AppConfig.class

package test; 

import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 

@Configuration 
@ComponentScan(basePackageClasses = AppConfig.class) 
@EnableJpaRepositories(basePackageClasses = Employee.class) 
public class AppConfig { 

} 

AppMain.class

package test; 

import java.util.List; 

import org.springframework.context.annotation.AnnotationConfigApplicationContext; 
import org.springframework.context.support.AbstractApplicationContext; 


public class AppMain { 

    public static void main(String args[]) { 
     AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); 

     EmployeeRepository service = (EmployeeRepository) context.getBean("AccountRepository"); 

     /* 
     * Create Employee1 
     */ 
     Employee employee1 = new Employee(); 
     employee1.setName("name1"); 

     /* 
     * Create Employee2 
     */ 
     Employee employee2 = new Employee(); 
     employee2.setName("name2"); 

     /* 
     * Persist both Employees 
     */ 
     service.save(employee1); 
     service.save(employee2); 

     List<Employee> emps = service.findAll(); 
     for (Employee emp : emps) { 
      System.out.println(emp); 
     } 

     context.close(); 
    } 
} 

Empoyee.class

package test; 


import java.io.Serializable; 
import java.util.Objects; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.Table; 


@Entity 
@Table(name = "EMPLOYEE") 
public class Employee implements Serializable { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private int id; 

    @Column(name = "NAME", nullable = false) 
    private String name; 


    public int getId() { 
     return id; 
    } 

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

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    @Override 
    public String toString() { 
     return "Employee{" + "id=" + id + ", name=" + name + '}'; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 5; 
     hash = 19 * hash + this.id; 
     hash = 19 * hash + Objects.hashCode(this.name); 
     return hash; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) { 
      return true; 
     } 
     if (obj == null) { 
      return false; 
     } 
     if (getClass() != obj.getClass()) { 
      return false; 
     } 
     final Employee other = (Employee) obj; 
     if (this.id != other.id) { 
      return false; 
     } 
     if (!Objects.equals(this.name, other.name)) { 
      return false; 
     } 
     return true; 
    } 
} 

版本库

package test; 

import java.util.List; 
import org.springframework.data.jpa.repository.JpaRepository; 
import org.springframework.stereotype.Repository; 

@Repository 
public interface EmployeeRepository extends JpaRepository<Employee, Long> { 

    Employee findOneByName(String name); 

    List<Employee> findAllByOrderByIdAsc(); 
} 

HibernateCOnfig

package test; 

import java.util.Properties; 

import javax.sql.DataSource; 

import org.hibernate.SessionFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.PropertySource; 
import org.springframework.core.env.Environment; 
import org.springframework.jdbc.datasource.DriverManagerDataSource; 
import org.springframework.orm.hibernate4.HibernateTransactionManager; 
import org.springframework.orm.hibernate4.LocalSessionFactoryBean; 
import org.springframework.transaction.annotation.EnableTransactionManagement; 

@Configuration 
@EnableTransactionManagement 
@PropertySource(value = {"classpath:application.properties"}) 
public class HibernateConfiguration { 

    @Autowired 
    private Environment environment; 

    @Bean 
    public LocalSessionFactoryBean sessionFactory() { 
     LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); 
     sessionFactory.setDataSource(dataSource()); 
     sessionFactory.setPackagesToScan(new String[]{"test"}); 
     sessionFactory.setHibernateProperties(hibernateProperties()); 
     return sessionFactory; 
    } 

    @Bean 
    public DataSource dataSource() { 
     DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
     dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName")); 
     dataSource.setUrl(environment.getRequiredProperty("jdbc.url")); 
     dataSource.setUsername(environment.getRequiredProperty("jdbc.username")); 
     dataSource.setPassword(environment.getRequiredProperty("jdbc.password")); 
     return dataSource; 
    } 

    private Properties hibernateProperties() { 
     Properties properties = new Properties(); 
     properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect")); 
     properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql")); 
     properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql")); 
     properties.put("hibernate.hbm2ddl.auto", "update"); 

     return properties; 
    } 

    @Bean 
    @Autowired 
    public HibernateTransactionManager transactionManager(SessionFactory s) { 
     HibernateTransactionManager txManager = new HibernateTransactionManager(); 
     txManager.setSessionFactory(s); 
     return txManager; 
    } 
} 

的application.properties

jdbc.driverClassName = org.h2.Driver 
jdbc.url = jdbc:h2:./test 
jdbc.username = myuser 
jdbc.password = mypassword 
hibernate.dialect = org.hibernate.dialect.H2Dialect 
hibernate.show_sql = false 
hibernate.format_sql = false 

最后pom.xml中

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 

    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.websystique.spring</groupId> 
    <artifactId>Spring4HibernateExample</artifactId> 
    <version>1.0.0</version> 
    <packaging>jar</packaging> 

    <name>Spring4HibernateExample</name> 

    <properties> 
     <springframework.version>4.0.6.RELEASE</springframework.version> 
     <hibernate.version>4.3.6.Final</hibernate.version> 
     <mysql.version>5.1.31</mysql.version> 
     <joda-time.version>2.3</joda-time.version> 
     <maven.compiler.source>1.8</maven.compiler.source> 
     <maven.compiler.target>1.8</maven.compiler.target> 
    </properties> 

    <dependencies> 

     <!-- Spring --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
      <version>${springframework.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>${springframework.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-tx</artifactId> 
      <version>${springframework.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-orm</artifactId> 
      <version>${springframework.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.data</groupId> 
      <artifactId>spring-data-jpa</artifactId> 
      <version>1.10.1.RELEASE</version> 
     </dependency> 

     <!-- Hibernate --> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-core</artifactId> 
      <version>${hibernate.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-jpamodelgen</artifactId> 
      <version>${hibernate.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate.javax.persistence</groupId> 
      <artifactId>hibernate-jpa-2.1-api</artifactId> 
      <version>1.0.0.Final</version> 
     </dependency>  

     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-entitymanager</artifactId> 
      <version>${hibernate.version}</version> 
     </dependency> 

     <dependency> 
      <groupId>com.h2database</groupId> 
      <artifactId>h2</artifactId> 
      <version>1.4.191</version> 
     </dependency> 
     <dependency> 
      <groupId>org.eclipse.persistence</groupId> 
      <artifactId>eclipselink</artifactId> 
      <version>2.5.2</version> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.5.1</version> 
       <configuration> 
        <source>1.8</source> 
        <target>1.8</target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 


</project> 

如果我尝试运行的例子,我得到: ...引起:java.lang.IllegalA rgumentException:必须存在至少一个JPA元模型!

但我可以在目标文件夹中看到自动生成的元模型文件。我错过了什么?

+1

为什么继续关于JPA元模型,当你不使用JPA API? SessionFactory/Session的东西是Hibernate API,并且在任何JPA应用程序 –

+0

中都没有地方,主要是因为我不知道,我认为它是需要的。你能剥夺所有无用的东西吗? –

+0

在基于JPA的应用程序中,您应该使用类似于LocalContainerEntityManagerFactoryBean的东西来构造您的EntityManager和一个接受您的EntityManagerFactory实例的JpaTransactionManager。 – Naros

回答

2

当我尝试使用hibernate-entitymanager版本5.2.1.Final通过spring-boot-starter-data-jpa 1.3.0.RELEASE进行spring启动时,看到相同的错误消息。

我也看到了target/generated-sources/annotations中生成的MetaModel对象。

使用较旧的4.3.11.Final版本正常工作 - 没有错误。对我来说,它看起来像春季启动和休眠版本之间的版本不兼容,也许这是有帮助的。

+0

我也在休眠5.2.1最终版本上发现了同样的问题,所以我升级到了5.2.2,现在它可以正常工作,使用spring boot 1.4.1 –