【问题标题】:spring-data-jpa @OneToMany fails with lazy initspring-data-jpa @OneToMany 因惰性初始化而失败
【发布时间】:2014-05-28 13:25:56
【问题描述】:

我正在尝试使用 Spring-data-jpa、hibernate、spring-data、H2(用于测试)和最终的 Postgress(生产)创建父子关系。

以下是 h2.sql 中定义的表:

CREATE TABLE IF NOT EXISTS Menu (
  menuId bigint(11) NOT NULL AUTO_INCREMENT,
  displayText varchar (100)  DEFAULT NOT NULL,
  displayOrder int default NULL
  );


CREATE TABLE IF NOT EXISTS MenuItem (
  menuItemId bigint(11) NOT NULL AUTO_INCREMENT,
  displayText varchar (100)  DEFAULT NOT NULL,
  path varchar (50)  NULL,
  toolTip varchar (500)  DEFAULT NOT NULL,
  displayOrder int default NULL,
  callType varchar (50)  DEFAULT NOT NULL
  );

我有两个简单的实体:

@Entity
public class Menu {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long menuId;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "menu")
    private List<MenuItem> menuItems = new ArrayList<MenuItem>();

    private String displayText;
    private int displayOrder;

@Entity
public class MenuItem {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long menuItemId;

    private String displayText;
    private String path;
    private String toolTip;
    private int displayOrder;

   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "menuId", nullable = false)
    private Menu menu;

    @Enumerated(EnumType.STRING)
    @Column(name = "callType", nullable = false)
    private HttpType callType;

我有一个应用程序:

@Configuration
@ComponentScan
@EnableJpaRepositories
@EnableTransactionManagement
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        ApplicationContext MyApplication = SpringApplication.run( Application.class, args );
    }
}

还有一个配置类:

@Configuration
public class MyConfiguration {
@Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
        LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
        lef.setDataSource( dataSource );
        lef.setJpaVendorAdapter( jpaVendorAdapter );
        lef.setPackagesToScan( "com.xxx.yyy" );
        return lef;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
        hibernateJpaVendorAdapter.setShowSql( true );
        hibernateJpaVendorAdapter.setGenerateDdl( true );
        hibernateJpaVendorAdapter.setDatabase( Database.H2 );
        return hibernateJpaVendorAdapter;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new JpaTransactionManager();
    }

    @Bean
    public DataSource dataSource() {

        return new EmbeddedDatabaseBuilder().setType( EmbeddedDatabaseType.H2 ).setName( "product" )
                .addScript( "classpath:h2.sql" ).build();
    }
}

我有一个测试:

@SpringApplicationConfiguration
@Transactional
class MenuRepositoryTest extends Specification {

    @Shared
    ConfigurableApplicationContext context

    @Shared
    private MenuRepository menuRepository

    void setupSpec() {
        Future future = Executors.newSingleThreadExecutor().submit(
                new Callable() {
                    @Override
                    public ConfigurableApplicationContext call() throws Exception {
                        return (ConfigurableApplicationContext) SpringApplication.run(Application.class)
                    }
                })
        context = future.get(60, TimeUnit.SECONDS)
        menuRepository = context.getBean(MenuRepository.class)
    }

    void cleanupSpec() {
        if (context != null) {
            context.close()
        }
    }
 @Transactional
    def "test creating a single menu with a single menuItem"() {

        def menu = new Menu()
        menu.setDisplayOrder(0)
        menu.setDisplayText("test")
        menuRepository.save(menu)

        def menuItem = new MenuItem()
        menuItem.setToolTip("tooltip 1")
        menuItem.setPath("/1")
        menuItem.setCallType(HttpType.GET)
        menuItem.setDisplayText("tooltip")
        menu.addMenuItem(menuItem)

        when:
        def menus = menuRepository.findAll()
        menus[0].getMenuItems()

        then:
        menus[0].getMenuItems().size() == 1

    }
}

这是我的 gradle 显示依赖项:

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'jacoco'
apply plugin: 'war'
apply plugin: 'maven'


buildscript {
    repositories {
        maven { url "http://repo.spring.io/libs-snapshot" }
        mavenLocal()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.0.RC4")
    }
}
repositories {
    mavenCentral()
    maven { url "http://repo.spring.io/libs-snapshot" }
    maven { url 'http://repo.spring.io/milestone' }
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:1.0.0.RELEASE")
    compile("org.springframework.boot:spring-boot-starter-data-jpa:1.0.1.RELEASE")
    compile("org.springframework.boot:spring-boot:1.0.1.RELEASE")
    compile("org.springframework:spring-orm:4.0.0.RC1")
    compile("org.hibernate:hibernate-entitymanager:4.2.1.Final")
    compile("org.springframework:spring-tx")
    compile("com.h2database:h2:1.3.172")
    compile("joda-time:joda-time:2.3")
    compile("org.thymeleaf:thymeleaf-spring4")
    compile("org.codehaus.groovy.modules.http-builder:http-builder:0.7.1")
    compile('org.codehaus.groovy:groovy-all:2.2.1')
    compile('org.jadira.usertype:usertype.jodatime:2.0.1')

    testCompile('org.spockframework:spock-core:0.7-groovy-2.0') {
        exclude group: 'org.codehaus.groovy', module: 'groovy-all'
    }
    testCompile('org.codehaus.groovy.modules.http-builder:http-builder:0.7+')
    testCompile("junit:junit")
}

jacocoTestReport {
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
}

sourceSets {

    main {

        java {
            srcDirs = []
        }
        groovy {
            srcDirs = ['src/main/groovy', 'src/main/java']
        }
        resources {
            srcDirs = ['src/main/resources']
        }

        output.resourcesDir = "build/classes/main"
    }

    test {
        java {
            srcDirs = []
        }
        groovy {
            srcDirs = ['src/test/groovy', 'src/test/java']
        }
        resources {
            srcDirs = ['src/test/resources']
        }

        output.resourcesDir = "build/classes/test"
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
}

回答

更改 build.gradle 以使用不同的 spock

buildscript {
    repositories {
        maven { url "http://repo.spring.io/libs-milestone" }
        mavenLocal()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.1.RELEASE")
    }
}
repositories {
    mavenCentral()
    maven { url "http://repo.spring.io/libs-milestone" }
    maven { url "https://repository.jboss.org/nexus/content/repositories/releases" }
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
    maven { url "http://repo.spring.io/snapshot" }
    maven { url 'http://repo.spring.io/milestone' }
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web:1.0..RELEASE")
    compile("org.springframework.boot:spring-boot:1.0.1.RELEASE")
    compile("org.springframework.boot:spring-boot-starter-data-jpa:1.0.1.RELEASE")
    testCompile('org.spockframework:spock-core:1.0-groovy-2.0-SNAPSHOT') {
        exclude group: 'org.codehaus.groovy', module: 'groovy-all'
    }

    testCompile('org.spockframework:spock-spring:1.0-groovy-2.0-SNAPSHOT') {
        exclude group: 'org.spockframework', module: 'spock-core'
        exclude group: 'org.spockframework', module: 'spring-beans'
        exclude group: 'org.spockframework', module: 'spring-test'
        exclude group: 'org.codehaus.groovy', module: 'groovy-all'
    }
    testCompile('org.springframework:spring-test:4.0.3.RELEASE')
...}

将测试更改为不使用 SetupSpec 或 @Shared:

@ContextConfiguration(classes = MyApplication, loader = SpringApplicationContextLoader)
@Transactional
class MenuRepositoryTest extends Specification {

    @Autowired
    private MenuRepository menuRepository

    def "test creating a single menu with a single menuItem"() {

        def menu = new Menu()
        menu.setDisplayOrder(0)
        menu.setDisplayText("test")
        menuRepository.save(menu)

        def menuItem = new MenuItem()
        menuItem.setToolTip("tooltip 1")
        menuItem.setPath("/1")
        menuItem.setCallType(HttpType.GET)
        menuItem.setDisplayText("tooltip")
        menu.addMenuItem(menuItem)

        when:
        def menus = menuRepository.findAll()
        menus[0].getMenuItems()

        then:
        menus[0].getMenuItems().size() == 1

    }
}

【问题讨论】:

  • 您的映射不正确:双向关联必须有一个所有者端和一个反向端,由 mappedBy 属性标记。在 OneToMany 中,一侧必须是反侧。当然,如果在 Menu 和 MenuItem 之间有一个 OneToMany,那么在 MenuItem 和 Menu 之间必须有一个 ManyToOne,而不是 OneToOne。还要注意targetEntity是没用的:列表是List&lt;MenuItem&gt;,所以Hibernate知道目标实体是MenuItem。
  • 我更改了映射,但仍然收到相同的错误消息。根据我见过的其他例子,这对我来说是正确的。这让我想知道它是否是在 spring-boot 中使用事务的方式,但这是一个疯狂的猜测。
  • Spock 可能对 Spring Boot 引导功能一无所知。它知道@Transactional吗?就您的引导使用而言,您的所有MyConfiguration 可能都可以删除(除非您可能需要@EntityScan 用于您的@Entities 所在的pacakges)。 Spring Boot 已经启用了@Transactional 并为你定义了一个事务管理器。
  • 我有另一个测试(未显示)将单个菜单插入和检索到 H2。只有@OneToMany 失败了。关于 MyConfiguration,我认为我需要那里,因为当我构建 WAR 并部署到 Tomcat 时,我最终会有一个 @Profile("test") 和 @Profile"production")。

标签: hibernate spring-data-jpa spring-boot


【解决方案1】:

这个问题的答案在于 Spock 和 Spring 的集成。映射都是正确的,但是 Spock 和 Spring 并没有很好地配合。我更新了问题以显示运行集成测试的正确方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 2017-09-23
    • 2019-12-17
    • 2011-10-23
    • 1970-01-01
    相关资源
    最近更新 更多