我被这个难倒,所以我在这里寻求帮助。
我正在开发一个带有多个数据库连接的jHipster应用程序,使用gradle构建。该应用运行良好的" ./ gradlew bootRun"但是当我尝试用" ./ gradlew -Pprod bootRepackage"打包一场独立的战争时并运行它,我得到这个例外:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [com/babycenter/belle/config/DatabaseConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Duplicate XML entry for com.babycenter.belle.readonly.domain.InvoiceLineItem
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1078)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
at com.babycenter.belle.BelleApp.main(BelleApp.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.WarLauncher.main(WarLauncher.java:59)
Caused by: java.lang.IllegalStateException: Duplicate XML entry for com.babycenter.belle.readonly.domain.InvoiceLineItem
at org.hibernate.cfg.annotations.reflection.XMLContext.addClass(XMLContext.java:140)
at org.hibernate.cfg.annotations.reflection.XMLContext.addDocument(XMLContext.java:105)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.<init>(AnnotationMetadataSourceProcessorImpl.java:94)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.<init>(MetadataBuildingProcess.java:147)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:141)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:858)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:885)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 22 common frames omitted
我感到困惑,因为它说错误的文件是DatabaseConfiguration.java,它应该只读取com.babycenter.belle.domain包中的实体,它所说的类/ XML是重复的,InvoiceLineItem,不在
以下是可能涉及的文件。告诉我你是否想看到另一个。
DatabaseConfiguration.java
package com.babycenter.belle.config;
import io.github.jhipster.config.JHipsterConstants;
import io.github.jhipster.config.liquibase.AsyncSpringLiquibase;
import liquibase.integration.spring.SpringLiquibase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.core.task.TaskExecutor;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
@Configuration
@EnableJpaRepositories(basePackages = "com.babycenter.belle.repository")
@EnableJpaAuditing(auditorAwareRef = "springSecurityAuditorAware")
@EnableTransactionManagement
public class DatabaseConfiguration {
private final Logger log = LoggerFactory.getLogger(DatabaseConfiguration.class);
private final Environment env;
public DatabaseConfiguration(Environment env) {
this.env = env;
}
@Bean
public SpringLiquibase liquibase(@Qualifier("taskExecutor") TaskExecutor taskExecutor, @Qualifier("dataSource") DataSource dataSource,
LiquibaseProperties liquibaseProperties) {
// Start Liquibase asynchronously
SpringLiquibase liquibase = new AsyncSpringLiquibase(taskExecutor, env);
// Start Liquibase synchronously
//SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setDataSource(dataSource);
liquibase.setChangeLog("classpath:config/liquibase/master.xml");
liquibase.setContexts(liquibaseProperties.getContexts());
liquibase.setDefaultSchema(liquibaseProperties.getDefaultSchema());
liquibase.setDropFirst(liquibaseProperties.isDropFirst());
if (env.acceptsProfiles(JHipsterConstants.SPRING_PROFILE_NO_LIQUIBASE)) {
liquibase.setShouldRun(false);
} else {
liquibase.setShouldRun(liquibaseProperties.isEnabled());
log.debug("Configuring Liquibase");
}
return liquibase;
}
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("dataSource") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.babycenter.belle.domain")
.persistenceUnit("readwrite")
.build();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
ReadOnlyDatabaseConfiguration.java
package com.babycenter.belle.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
@Configuration
@EnableJpaRepositories(
entityManagerFactoryRef = "roEntityManagerFactory",
transactionManagerRef = "roTransactionManager",
basePackages = "com.babycenter.belle.readonly.repository")
@EnableJpaAuditing(auditorAwareRef = "springSecurityAuditorAware")
@EnableTransactionManagement
public class ReadOnlyDatabaseConfiguration {
private final Logger log = LoggerFactory.getLogger(ReadOnlyDatabaseConfiguration.class);
@Bean(name = "roDataSource")
@ConfigurationProperties(prefix = "spring.datasource-ro")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "roEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean roEntityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("roDataSource") DataSource dataSource) {
return
builder
.dataSource(dataSource)
.packages("com.babycenter.belle.readonly.domain")
.persistenceUnit("readonly")
.build();
}
@Bean(name = "roTransactionManager")
public PlatformTransactionManager roTransactionManager(@Qualifier("roEntityManagerFactory") EntityManagerFactory roEntityManagerFactory) {
return new JpaTransactionManager(roEntityManagerFactory);
}
}
InvoiceLineItem.java
package com.babycenter.belle.readonly.domain;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import javax.persistence.*;
import java.io.Serializable;
/**
* An object representation of a row from the opr_invoice_line_item table in Redshift
*/
@Entity
@Table(name = "opr_invoice_line_item", schema = "advertising")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class InvoiceLineItem implements Serializable {
@Id
@Column(name = "invoice_line_item_id", insertable = false, updatable = false)
private Long invoiceLineItemId;
@Column(name = "sales_order_line_item_id", insertable = false, updatable = false)
private Long salesOrderLineItemId;
@Column(name = "primary_performance", insertable = false, updatable = false)
private Long primaryPerformance;
@Column(name = "third_party_performance", insertable = false, updatable = false)
private Long thirdPartyPerformance;
@Column(name = "invoice_units", insertable = false, updatable = false)
private String invoiceUnits;
@Column(name = "invoice_amount", insertable = false, updatable = false)
private Float invoiceAmount;
/**
* Assigned when possible - not a column in opr_invoice_line_item table
*/
private String billingPeriodName;
public Long getInvoiceLineItemId() {
return invoiceLineItemId;
}
public void setInvoiceLineItemId(Long invoiceLineItemId) {
this.invoiceLineItemId = invoiceLineItemId;
}
public Long getSalesOrderLineItemId() {
return salesOrderLineItemId;
}
public void setSalesOrderLineItemId(Long salesOrderLineItemId) {
this.salesOrderLineItemId = salesOrderLineItemId;
}
public Long getPrimaryPerformance() {
return primaryPerformance;
}
public void setPrimaryPerformance(Long primaryPerformance) {
this.primaryPerformance = primaryPerformance;
}
public Long getThirdPartyPerformance() {
return thirdPartyPerformance;
}
public void setThirdPartyPerformance(Long thirdPartyPerformance) {
this.thirdPartyPerformance = thirdPartyPerformance;
}
public String getInvoiceUnits() {
return invoiceUnits;
}
public void setInvoiceUnits(String invoiceUnits) {
this.invoiceUnits = invoiceUnits;
}
public Float getInvoiceAmount() {
return invoiceAmount;
}
public void setInvoiceAmount(Float invoiceAmount) {
this.invoiceAmount = invoiceAmount;
}
public String getBillingPeriodName() {
return billingPeriodName;
}
public void setBillingPeriodName(String billingPeriodName) {
this.billingPeriodName = billingPeriodName;
}
}
的build.gradle
import org.gradle.internal.os.OperatingSystem
buildscript {
repositories {
mavenLocal()
jcenter()
maven { url "http://repo.spring.io/plugins-release" }
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${spring_boot_version}"
classpath "org.springframework.build.gradle:propdeps-plugin:0.0.7"
//jhipster-needle-gradle-buildscript-dependency - JHipster will add additional gradle build script plugins here
}
}
plugins {
id "org.sonarqube" version "2.5"
id "net.ltgt.apt" version "0.11"
id "io.spring.dependency-management" version "1.0.3.RELEASE"
id "com.moowork.node" version "1.2.0"
//jhipster-needle-gradle-plugins - JHipster will add additional gradle plugins here
}
apply plugin: 'java'
sourceCompatibility=1.8
targetCompatibility=1.8
apply plugin: 'maven'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
apply plugin: 'propdeps'
apply plugin: 'com.moowork.node'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'idea'
defaultTasks 'bootRun'
group = 'com.babycenter.belle'
version = '0.0.1-SNAPSHOT'
description = ''
bootRepackage {
mainClass = 'com.babycenter.belle.BelleApp'
}
war {
}
springBoot {
mainClass = 'com.babycenter.belle.BelleApp'
executable = true
buildInfo()
}
if (OperatingSystem.current().isWindows()) {
task pathingJar(type: Jar) {
dependsOn configurations.runtime
appendix = 'pathing'
doFirst {
manifest {
attributes 'Class-Path': configurations.runtime.files.collect {
it.toURL().toString().replaceFirst(/file:\/+/, '/').replaceAll(' ', '%20')
}.join(' ')
}
}
}
bootRun {
addResources = false
dependsOn pathingJar
doFirst {
classpath = files("$buildDir/classes/java/main", "$buildDir/resources/main", pathingJar.archivePath)
}
}
} else {
bootRun {
addResources = false
systemProperties System.properties
}
}
//use jrebel to hot-deploy class changes during development
if (project.hasProperty('rebelAgent')) {
bootRun.jvmArgs += rebelAgent
}
test {
include '**/*UnitTest*'
include '**/*IntTest*'
// uncomment if the tests reports are not generated
// see https://github.com/jhipster/generator-jhipster/pull/2771 and https://github.com/jhipster/generator-jhipster/pull/4484
// ignoreFailures true
reports.html.enabled = false
}
task cucumberTest(type: Test) {
include '**/CucumberTest*'
// uncomment if the tests reports are not generated
// see https://github.com/jhipster/generator-jhipster/pull/2771 and https://github.com/jhipster/generator-jhipster/pull/4484
// ignoreFailures true
reports.html.enabled = false
}
test.finalizedBy(cucumberTest)
task testReport(type: TestReport) {
destinationDir = file("$buildDir/reports/tests")
reportOn test
reportOn cucumberTest
}
cucumberTest.finalizedBy(testReport)
apply from: 'gradle/docker.gradle'
apply from: 'gradle/sonar.gradle'
apply from: 'gradle/liquibase.gradle'
apply from: 'gradle/mapstruct.gradle'
//jhipster-needle-gradle-apply-from - JHipster will add additional gradle scripts to be applied here
if (project.hasProperty('prod')) {
apply from: 'gradle/profile_prod.gradle'
} else if (project.hasProperty('stag')) {
apply from: 'gradle/profile_stag.gradle'
} else {
apply from: 'gradle/profile_dev.gradle'
}
if (project.hasProperty('graphite')) {
apply from: 'gradle/graphite.gradle'
}
if (project.hasProperty('prometheus')) {
apply from: 'gradle/prometheus.gradle'
}
configurations {
providedRuntime
compile.exclude module: "spring-boot-starter-tomcat"
}
repositories {
mavenLocal()
jcenter()
maven { url 'http://nexus.babycenter.com:8081/nexus/content/repositories/thirdparty/'}
}
dependencies {
compile "io.github.jhipster:jhipster:${jhipster_server_version}"
compile "io.dropwizard.metrics:metrics-core:${dropwizard_metrics_version}"
compile "io.dropwizard.metrics:metrics-jcache:${dropwizard_metrics_version}"
compile "io.dropwizard.metrics:metrics-jvm:${dropwizard_metrics_version}"
compile "io.dropwizard.metrics:metrics-servlet:${dropwizard_metrics_version}"
compile "io.dropwizard.metrics:metrics-json:${dropwizard_metrics_version}"
compile "io.dropwizard.metrics:metrics-servlets:${dropwizard_metrics_version}"
compile "net.logstash.logback:logstash-logback-encoder:${logstash_logback_encoder_version}"
compile "com.fasterxml.jackson.dataformat:jackson-dataformat-csv:${jackson_dataformat_csv_version}"
compile "com.fasterxml.jackson.datatype:jackson-datatype-json-org"
compile "com.fasterxml.jackson.datatype:jackson-datatype-hppc"
compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310"
compile "com.fasterxml.jackson.datatype:jackson-datatype-hibernate5"
compile "com.fasterxml.jackson.core:jackson-annotations"
compile "com.fasterxml.jackson.core:jackson-databind"
compile "com.fasterxml.jackson.module:jackson-module-afterburner"
compile ("com.ryantenney.metrics:metrics-spring:${metrics_spring_version}")
compile "javax.cache:cache-api"
compile "org.hibernate:hibernate-core:${hibernate_version}"
compile ("com.zaxxer:HikariCP:${hikaricp_version}")
compile "org.apache.commons:commons-lang3:${commons_lang_version}"
compile "commons-io:commons-io:${commons_io_version}"
compile "javax.transaction:javax.transaction-api"
compile "org.ehcache:ehcache"
compile "org.hibernate:hibernate-jcache:${hibernate_version}"
compile "org.hibernate:hibernate-envers:${hibernate_version}"
compile "org.hibernate:hibernate-validator"
compile "org.hibernate:hibernate-entitymanager:${hibernate_version}"
compile ("org.liquibase:liquibase-core")
compile "com.mattbertolini:liquibase-slf4j:${liquibase_slf4j_version}"
compile "org.springframework.boot:spring-boot-actuator"
compile "org.springframework.boot:spring-boot-autoconfigure"
compile "org.springframework.boot:spring-boot-loader-tools"
compile "org.springframework.boot:spring-boot-starter-mail"
compile "org.springframework.boot:spring-boot-starter-logging"
compile "org.springframework.boot:spring-boot-starter-aop"
compile "org.springframework.boot:spring-boot-starter-data-jpa"
compile "org.springframework.boot:spring-boot-starter-security"
compile ("org.springframework.boot:spring-boot-starter-web") {
exclude module: 'spring-boot-starter-tomcat'
}
compile "org.springframework.boot:spring-boot-starter-undertow"
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
compile "org.zalando:problem-spring-web:${problem_spring_web_version}"
compile "org.springframework.boot:spring-boot-starter-cloud-connectors"
compile ("org.springframework:spring-context-support")
compile "org.springframework.security:spring-security-config"
compile "org.springframework.security:spring-security-data"
compile "org.springframework.security:spring-security-web"
compile ("io.springfox:springfox-swagger2:${springfox_version}") {
exclude module: 'mapstruct'
}
compile "io.springfox:springfox-bean-validators:${springfox_version}"
//The Data Warehouse (i.e. our read-only data source) is AWS Redshift
compile "com.amazon.redshift:RedShiftJDBC42:${redshift_jdbc_version}" //needs the RedShiftJDBC42 driver to work
compile "com.amazonaws:aws-java-sdk-redshift:${redshift_sdk_version}"
//our writable database is mysql
compile "mysql:mysql-connector-java"
compile "org.mapstruct:mapstruct-jdk8:${mapstruct_version}"
testCompile "org.awaitility:awaitility:${awaitility_version}"
testCompile "com.jayway.jsonpath:json-path"
testCompile "info.cukes:cucumber-junit:${cucumber_version}"
testCompile "info.cukes:cucumber-spring:${cucumber_version}"
testCompile ("org.springframework.boot:spring-boot-starter-test") {
exclude group: 'com.vaadin.external.google', module: 'android-json'
}
testCompile "org.springframework.security:spring-security-test"
testCompile "org.springframework.boot:spring-boot-test"
testCompile "org.assertj:assertj-core:${assertj_version}"
testCompile "junit:junit"
testCompile "org.mockito:mockito-core"
testCompile "com.mattbertolini:liquibase-slf4j:${liquibase_slf4j_version}"
testCompile "org.hamcrest:hamcrest-library"
testCompile "com.h2database:h2"
optional ("org.springframework.boot:spring-boot-configuration-processor:${spring_boot_version}") {
exclude group: 'com.vaadin.external.google', module: 'android-json'
}
//jhipster-needle-gradle-dependency - JHipster will add additional dependencies here
}
clean {
delete "target"
}
task cleanResources(type: Delete) {
delete 'build/resources'
}
task wrapper(type: Wrapper) {
gradleVersion = '4.1'
}
task stage(dependsOn: 'bootRepackage') {
}
if (project.hasProperty('nodeInstall')) {
node {
version = "${node_version}"
npmVersion = "${npm_version}"
yarnVersion = "${yarn_version}"
download = true
}
}
compileJava.dependsOn processResources
processResources.dependsOn cleanResources,bootBuildInfo
bootBuildInfo.mustRunAfter cleanResources
以下是我的版本:
jHipster 4.9.0
Spring Boot 1.5.8.RELEASE
Gradle 4.2
Groovy 2.4.11
答案 0 :(得分:1)
我最终能够自己解决这个问题。
我在代码中使用注释来定义我的JPA实体PLUS一个orm.xml文件,用于必须定义我的结果类实体的命名查询。我没有persistence.xml文件。
这与“./gradlew bootRun”运行良好,但是当我捆绑一个战争时,它抛出了上面列出的异常。
我了解到,当我使用Hibernate JPA打包Spring Boot war时,它会尝试将带注释的实体添加到orm.xml中 - 甚至可能多次加载orm.xml:
Duplicate XML entry | Hibernate orm.xml
由于我已经有一个实体定义,它说有重复。
基本上我学会了我可以在代码中使用注释,包括命名查询,或者在orm.xml中定义所有内容,但我不能将两者混合使用。