在Spring Boot应用程序中集成SQLServer和H2

时间:2018-01-18 19:28:03

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

我正在尝试将SQL Server 2016和H2内存数据库集成到我的一个Spring Boot项目中。所有配置都已设置,我可以为两个数据库加载持久性单元而不会出现任何问题。此外,我可以查询SQL Server数据库,我也可以获取我的数据。 Sql Server JPA配置是我的主要配置。但是,不知何故,JPA无法在H2数据库中创建实体。在看到日志之后,我得出的结论是,我尝试在H2中创建的实体实际上是在SqlServer Datbase中尝试找到它。这个抱怨的日志的一部分如下:

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'person'.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:232)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1672)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:460)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:405)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7535)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2438)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:208)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:183)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:348)
    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.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
    at com.sun.proxy.$Proxy152.executeUpdate(Unknown Source)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
    ... 32 common frames omitted

更多详细信息日志发布如下。以下是我迄今为止所做和配置的课程。

application.properties

#Profile Properties
spring.profiles.active=local

# DataSource Properties for SQL Server
app.sqlserver.datasource.url=jdbc:sqlserver://XYZ;database=PQR
app.sqlserver.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
app.sqlserver.jpa.properties.hibernate.ddl-auto=update
app.sqlserver.jpa.properties.hibernate.format_sql=true
app.sqlserver.jpa.properties.hibernate.dialect=<custom_dialect_class>
app.sqlserver.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
app.sqlserver.jpa.show-sql=true

# DataSource Properties for H2
app.h2.datasource.url=jdbc:h2:mem:~/h2/TMV
app.h2.datasource.platform=h2
app.h2.datasource.username=sa
app.h2.datasource.password=
app.h2.datasource.driver-class-name=org.h2.Driver
app.h2.jpa.properties.hibernate.hbm2ddl.auto=create #here I tried using ddl-auto=create
app.h2.jpa.properties.hibernate.format_sql=true
app.h2.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
app.h2.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
app.h2.jpa.show-sql=true
spring.h2.console.enabled=true
spring.h2.console.path=/h2_console

SQLServerDataSourceConfig.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
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.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

@Configuration
@EnableJpaRepositories(
        entityManagerFactoryRef = "sqlServerEntityManager",
        basePackages = "com.xyz.persistence.dao",
        transactionManagerRef = "sqlServerTransactionManager"
)
public class SQLServerDataSourceConfig {

    @Autowired(required = false)
    private PersistenceUnitManager persistenceUnitManager;

    @Bean
    @ConfigurationProperties("app.sqlserver.jpa")
    public JpaProperties sqlServerJpaProperties(){
        return new JpaProperties();
    }

    @Bean
    @ConfigurationProperties("app.sqlserver.datasource")
    public DataSource sqlServerDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public LocalContainerEntityManagerFactoryBean sqlServerEntityManager(
            JpaProperties sqlServerJpaProperties) {
        EntityManagerFactoryBuilder builder = createEntityManagerFactoryBuilder(sqlServerJpaProperties);
        return builder
                .dataSource(sqlServerDataSource())
                .packages("com.xyz.persistence.domain")
                .persistenceUnit("SQLServer-PU")
                .build();
    }

    @Bean
    @Primary
    public JpaTransactionManager sqlServerTransactionManager(EntityManagerFactory sqlServerEntityManager) {
        return new JpaTransactionManager(sqlServerEntityManager);
    }

    private EntityManagerFactoryBuilder createEntityManagerFactoryBuilder(JpaProperties sqlServerJpaProperties) {
        JpaVendorAdapter jpaVendorAdapter = createJpaVendorAdapter(sqlServerJpaProperties);
        return new EntityManagerFactoryBuilder(jpaVendorAdapter,
                sqlServerJpaProperties.getProperties(), this.persistenceUnitManager);
    }

    private JpaVendorAdapter createJpaVendorAdapter(JpaProperties jpaProperties) {
        AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setShowSql(jpaProperties.isShowSql());
        adapter.setDatabase(jpaProperties.getDatabase());
        adapter.setDatabasePlatform(jpaProperties.getDatabasePlatform());
        adapter.setGenerateDdl(jpaProperties.isGenerateDdl());
        return adapter;
    }
}

H2DataSourceConfig.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
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.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

@Configuration
@EnableJpaRepositories(
        entityManagerFactoryRef = "h2EntityManager",
        basePackages = "com.xyz.persistence.daoh2",
        transactionManagerRef = "h2TransactionManager"
)
@EntityScan(basePackages = {"com.xyz.persistence.domainh2"})
public class H2DataSourceConfig {

    @Autowired(required = false)
    private PersistenceUnitManager persistenceUnitManager;

    @Bean
    @ConfigurationProperties("app.h2.jpa")
    public JpaProperties sqlServerJpaProperties() {
        return new JpaProperties();
    }

    @Bean
    @ConfigurationProperties("app.h2.datasource")
    public DataSource sqlServerDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean h2EntityManager(
            JpaProperties h2JpaProperties) {
        EntityManagerFactoryBuilder builder = createEntityManagerFactoryBuilder(h2JpaProperties);
        return builder
                .dataSource(sqlServerDataSource())
                .packages("com.xyz.persistence.domainh2")
                .persistenceUnit("H2-PU")
                .build();
    }

    @Bean
    public JpaTransactionManager h2TransactionManager(EntityManagerFactory h2EntityManager) {
        return new JpaTransactionManager(h2EntityManager);
    }

    private EntityManagerFactoryBuilder createEntityManagerFactoryBuilder(JpaProperties h2JpaProperties) {
        JpaVendorAdapter jpaVendorAdapter = createJpaVendorAdapter(h2JpaProperties);
        return new EntityManagerFactoryBuilder(jpaVendorAdapter,
                h2JpaProperties.getProperties(), this.persistenceUnitManager);
    }

    private JpaVendorAdapter createJpaVendorAdapter(JpaProperties jpaProperties) {
        AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setShowSql(jpaProperties.isShowSql());
        adapter.setDatabase(jpaProperties.getDatabase());
        adapter.setDatabasePlatform(jpaProperties.getDatabasePlatform());
        adapter.setGenerateDdl(jpaProperties.isGenerateDdl());
        return adapter;
    }
}

PersonDao.java是“com.xyz.persistence.daoh2

中的类
import com.xyz.persistence.domainh2.Person;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface PersonDao extends CrudRepository<Person, Long> {
}

CustomerDao.java是“com.xyz.persistence.dao

中的类
import com.xyz.persistence.domain.Customer;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CustomerDao extends CrudRepository<Customer, Long> {

}

Person.class

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "PERSON")
public class Person implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy =  GenerationType.AUTO)
    @Column(name = "ID")
    private Long id;

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

    @Column(name = "LAST_NAME",nullable = false)
    private String lastName;
}

Customer.class

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "CUSTOMER")
public class Customer implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy =  GenerationType.AUTO)
    @Column(name = "ID")
    private Long id;

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

    @Column(name = "LAST_NAME",nullable = false)
    private String lastName;
}

这是我的H2DataLoader.java类,implements CommandLineRunner在部署时import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.context.annotation.Configuration; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; @Configuration @Slf4j public class H2DataLoader implements CommandLineRunner { @PersistenceContext(name = "h2EntityManager") private EntityManager entityManager; @Override public void run(String... args) throws Exception { try { Query query = entityManager.createNativeQuery("INSERT INTO PERSON (ID, FIRST_NAME, LAST_NAME) VALUES (?, ?, ?)"); query.setParameter(1, 1L); query.setParameter(2, "Hello"); query.setParameter(3, "World"); query.executeUpdate(); } catch (Exception e) { log.error("Error Occurred: {}", e); } } } 运行此类。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication(
        scanBasePackages = { "com.xyz.persistence" },
        exclude = HibernateJpaAutoConfiguration.class
)
@EnableTransactionManagement
@EnableAsync
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class ApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApiApplication.class, args);
    }
}

Spring Boot的主要ApiApplication.java

build.gradle

最后这是我的buildscript { ext { springBootVersion = '1.5.6.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse-wtp' apply plugin: 'org.springframework.boot' apply plugin: 'war' sourceCompatibility = 1.8 repositories { mavenCentral() } configurations { providedRuntime } dependencies { compile("org.springframework.boot:spring-boot-starter-web") compile('org.springframework.boot:spring-boot-starter-aop') compile('org.springframework.boot:spring-boot-starter-data-jpa') runtime('org.springframework.boot:spring-boot-devtools') compile('com.microsoft.sqlserver:mssql-jdbc') compileOnly('org.projectlombok:lombok') runtime('com.h2database:h2') } 内容:

.....
    [INFO ] 2018-01-18 11:38:41.984 [restartedMain] o.s.o.j.LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'H2-PU'
    [INFO ] 2018-01-18 11:38:44.950 [restartedMain] o.s.o.j.LocalContainerEntityManagerFactoryBean - Initialized JPA EntityManagerFactory for persistence unit 'H2-PU'
    [INFO ] 2018-01-18 11:38:45.043 [restartedMain] o.s.o.j.LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'SQLServer-PU'
    [INFO ] 2018-01-18 11:38:46.059 [restartedMain] o.s.o.j.LocalContainerEntityManagerFactoryBean - Initialized JPA EntityManagerFactory for persistence unit 'SQLServer-PU'
    [INFO ] 2018-01-18 11:38:48.122 [restartedMain] o.s.a.f.CglibAopProxy - Final method [public final org.springframework.http.ResponseEntity org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.handleException(java.lang.Exception,org.springframework.web.context.request.WebRequest)] cannot get proxied via CGLIB: Calls to this method will NOT be routed to the target instance and might lead to NPEs against uninitialized fields in the proxy instance.
    ……
    Hiding endpoints mapping details, because this is my official project.
    ……
    o.s.b.c.e.t.TomcatEmbeddedServletContainer - Tomcat started on port(s): 8084 (http)
    Hibernate: 
        INSERT 
        INTO
            person
            (ID, FIRST_NAME, LAST_NAME) 
        VALUES
            (?, ?, ?)
    [WARN ] 2018-01-18 11:38:54.600 [restartedMain] o.h.e.j.s.SqlExceptionHelper - SQL Error: 208, SQLState: S0002
    [ERROR] 2018-01-18 11:38:54.600 [restartedMain] o.h.e.j.s.SqlExceptionHelper - Invalid object name 'person'.
    [ERROR] 2018-01-18 11:38:54.615 [restartedMain] c.h.m.p.c.H2DataLoader - Error Occurred: {}
    javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602)
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:1700)
        at org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:70)
        at com.hms.matching.persistence.config.H2DataLoader.run(H2DataLoader.java:34)
        at com.hms.matching.persistence.config.H2DataLoader$$FastClassBySpringCGLIB$$28072180.invoke(<generated>)
        at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
        at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
        at com.hms.matching.persistence.config.H2DataLoader$$EnhancerBySpringCGLIB$$d87b2678.run(<generated>)
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:732)
        at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:716)
        at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:703)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:304)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
        at com…...api.ApiApplication.main(ApiApplication.java:24)
        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.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
    Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement
        at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
        at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
        at org.hibernate.engine.query.spi.NativeSQLQueryPlan.performExecuteUpdate(NativeSQLQueryPlan.java:194)
        at org.hibernate.internal.SessionImpl.executeNativeUpdate(SessionImpl.java:1373)
        at org.hibernate.internal.SQLQueryImpl.executeUpdate(SQLQueryImpl.java:373)
        at org.hibernate.jpa.internal.QueryImpl.internalExecuteUpdate(QueryImpl.java:405)
        at org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:61)
        ... 27 common frames omitted
    Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'person'.
        at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:232)
        at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1672)
        at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:460)
        at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:405)
        at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7535)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2438)
        at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:208)
        at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:183)
        at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:348)
        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.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
        at com.sun.proxy.$Proxy152.executeUpdate(Unknown Source)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
        ... 32 common frames omitted
    [INFO ] 2018-01-18 11:38:54.631 [restartedMain] o.s.b.a.l.AutoConfigurationReportLoggingInitializer - 

    Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
    [ERROR] 2018-01-18 11:38:54.662 [restartedMain] o.s.b.SpringApplication - Application startup failed
    java.lang.IllegalStateException: Failed to execute CommandLineRunner
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:735)
        at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:716)
        at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:703)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:304)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
        at com……….api.ApiApplication.main(ApiApplication.java:24)
        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.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
    Caused by: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly
        at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:526)
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:504)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:292)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
        at com.hms.matching.persistence.config.H2DataLoader$$EnhancerBySpringCGLIB$$d87b2678.run(<generated>)
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:732)
        ... 11 common frames omitted
    Caused by: javax.persistence.RollbackException: Transaction marked as rollbackOnly
        at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:58)
        at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517)
        ... 22 common frames omitted
    [INFO ] 2018-01-18 11:38:54.662 [restartedMain] o.s.b.c.e.AnnotationConfigEmbeddedWebApplicationContext - Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7ab1fbb9: startup date [Thu Jan 18 11:38:31 CST 2018]; root of context hierarchy

在运行此应用程序时,它在控制台中提供以下信息/错误:

=DSum("[Voucher_Balance]"|"tblVoucherInfo"|"[Patient_number] = " & [No])

我不知道为什么它试图在SQL Server中查找Person对象。有没有人对此有所了解。我在过去24小时内一直在关注这个问题。但是,没有找到任何东西。应在H2数据库中创建Person表。一切都很好SQL Server端。

1 个答案:

答案 0 :(得分:0)

我不知道这是否能解决您的问题,但是:

这不是JPA的工作方式。而是创建一个new Person()对象,该对象应为@Entity,并设置其属性,然后entityManager.persist(person)