2014-03-13 79 views
0

我試圖爲我的JUnit,Spring,Hibernate環境設置一個簡單的測試環境。我也嘗試在Java中配置整個事物,並暫時避開XML文件。Spring,Hibernate,JUnit帶註釋的實體

到目前爲止,我能夠@Autowire@Beans,但我不能讓@Entity可用。我不斷收到一個異常,說我的實體沒有註冊。

下面是我在做什麼的樣本:

JpaTestConfig.java

package com.springtest.test.configuration; 

import java.util.Properties; 
import javax.sql.DataSource; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; 
import org.springframework.jdbc.datasource.DriverManagerDataSource; 
import org.springframework.orm.jpa.JpaTransactionManager; 
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; 
import org.springframework.transaction.PlatformTransactionManager; 
import org.springframework.transaction.annotation.EnableTransactionManagement; 

@Configuration 
@EnableTransactionManagement 
public class JpaTestConfig { 


    @Bean 
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(){ 

     LocalContainerEntityManagerFactoryBean lcemfb 
      = new LocalContainerEntityManagerFactoryBean(); 

     lcemfb.setDataSource(this.dataSource()); 
     lcemfb.setPackagesToScan(new String[] {"com.jverstry"}); 
    lcemfb.setPersistenceUnitName("MyTestPU"); 

     HibernateJpaVendorAdapter va = new HibernateJpaVendorAdapter(); 
    lcemfb.setJpaVendorAdapter(va); 

    Properties ps = new Properties(); 
    ps.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect"); 
    ps.put("hibernate.hbm2ddl.auto", "create"); 
    lcemfb.setJpaProperties(ps); 

    lcemfb.afterPropertiesSet(); 


     return lcemfb; 

    } 

    @Bean 
    public DataSource dataSource(){ 

     DriverManagerDataSource ds = new DriverManagerDataSource(); 

     ds.setDriverClassName("org.hsqldb.jdbcDriver"); 
     ds.setUrl("jdbc:hsqldb:mem:testdb"); 
     ds.setUsername("sa"); 
     ds.setPassword(""); 

     return ds; 

    } 

    @Bean 
    public PlatformTransactionManager transactionManager(){ 

     JpaTransactionManager tm = new JpaTransactionManager(); 
     tm.setEntityManagerFactory(this.entityManagerFactoryBean().getObject()); 

     return tm; 

    } 

    @Bean 
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){ 
     return new PersistenceExceptionTranslationPostProcessor(); 
    } 

} 

ServiceConfig.java

package com.springtest.test.configuration; 

import com.springtest.services.UserService; 
import com.springtest.services.UserServiceImpl; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 

@Configuration 
@ComponentScan(basePackages = { 
    "com.springtest.service" 
}) 
public class ServiceConfig { 

    @Bean 
    public UserService getuserService() { 
     return new UserServiceImpl(); 
    } 

} 

我的測試文件。 UserServiceTest.java

package com.springtest.test.services; 

import static org.junit.Assert.*; 
import org.junit.Test; 
import org.springframework.beans.factory.annotation.Autowired; 
import com.springtest.pojo.User; 
import com.springtest.services.UserService; 
import org.junit.runner.RunWith; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
import com.springtest.test.configuration.*; 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes={ JpaTestConfig.class, 
       ServiceConfig.class}) 

public class UserServiceTest { 

    @Autowired 
    private UserService userService; 

    @Test 
    public void testCreateAndRetrieve() { 
    String json = "{\"firstName\": \"John\", \"lastName\": \"Doe\"}"; 
    User user = userService.create(json); 
    assertEquals("John", user.getFirstName()); 
    } 
} 

我的服務豆:

package com.springtest.services; 

    import java.util.List; 

    import javax.persistence.EntityManager; 
    import javax.persistence.PersistenceContext; 
    import javax.persistence.PersistenceContextType; 
    import javax.persistence.criteria.CriteriaQuery; 
    import org.springframework.stereotype.Service; 
    import org.springframework.transaction.annotation.Transactional; 
    import com.google.gson.GsonBuilder; 
    import com.springtest.pojo.User; 

    @Service 
    public class UserServiceImpl implements UserService { 

     @PersistenceContext(type=PersistenceContextType.EXTENDED) 
     EntityManager em; 

     @Transactional 
     public User create(String json) { 
      User user = new GsonBuilder().create().fromJson(json, User.class); 
      em.persist(user); 
      return user; 
     } 

    } 

我的實體類:

package com.springtest.pojo; 

import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 

@Entity 
public class User { 

    @Id 
    @GeneratedValue 
    private Integer id; 
    private String firstName; 
    private String lastName; 

    public Integer getId() { 
     return id; 
    } 

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

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

} 

在總結,我怎麼註冊實體上我的Java類@Configuration文件?

感謝,

+1

你有'lcemfb.setPackagesToScan(new String [] {「com.jverstry」});'我看不到任何類/實體包'com.jverstry' – Rembo

回答

1

問題是與我的JpaTestConfiguration文件的包掃描儀。更具體地講,這條線:

lcemfb.setPackagesToScan(new String[] {"com.springtest.pojo"}); 

是一個將瀏覽包@Entity註釋類。謝謝@Rembo的提示。