如何将持久性单元的JPA EntityManagerFactory设置为“default”

时间:2017-05-23 06:32:18

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

Noramlly Spring-boot的所有默认配置都完全没问题。但是现在我需要将我的应用程序部署到Weblogic 12.2服务器上,并且我遇到了一些有关持久性的问题。

我只是想知道如何将持久性单元的JPA EntityManagerFactory设置为'default'。因为通过启动应用程序,我可以看到这些日志:

2017-05-23 08:16:34.608  INFO 30827 - j.LocalContainerEntityManagerFactoryBean []: Building JPA container EntityManagerFactory for persistence unit 'default'
2017-05-23 08:16:35.090  INFO 30827 - o.hibernate.jpa.internal.util.LogHelper  []: HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2017-05-23 08:16:36.025  INFO 30827 - org.hibernate.Version                    []: HHH000412: Hibernate Core {5.0.11.Final}
2017-05-23 08:16:36.026  INFO 30827 - org.hibernate.cfg.Environment            []: HHH000206: hibernate.properties not found
2017-05-23 08:16:36.029  INFO 30827 - org.hibernate.cfg.Environment            []: HHH000021: Bytecode provider name : javassist
2017-05-23 08:16:36.925  INFO 30827 - o.hibernate.annotations.common.Version   []: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-05-23 08:16:39.632  INFO 30827 - org.hibernate.dialect.Dialect            []: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
2017-05-23 08:16:48.205  INFO 30827 - j.LocalContainerEntityManagerFactoryBean []: Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-05-23 08:16:55.348  INFO 30827 - o.h.h.i.QueryTranslatorFactoryInitiator  []: HHH000397: Using ASTQueryTranslatorFactory

所以我不确定weblogic是使用默认持久性单元(eclipselink)还是真正休眠。所以我想在我的spring-boot应用程序中的任何地方设置持久性单元表达性。

有没有办法将hibernate设置为默认单位显式?

修改

之前我问过这个问题,因为我在运行时遇到异常。 org.eclipse.persistence库用于解组xml。

Caused by: org.eclipse.persistence.exceptions.DescriptorException: 
Exception Description: No conversion value provided for the value [ToConfirm] in field [@SubStatus].
Mapping: org.eclipse.persistence.oxm.mappings.XMLDirectMapping[subStatus-->@SubStatus]
Descriptor: XMLDescriptor(at.myCompany.bookingHubClient.schema.BookingHubUserTypes.BookResponse --> [DatabaseTable(BookResponse)])
    at org.eclipse.persistence.exceptions.DescriptorException.noFieldValueConversionToAttributeValueProvided(DescriptorException.java:1066)
    at org.eclipse.persistence.mappings.converters.ObjectTypeConverter.convertDataValueToObjectValue(ObjectTypeConverter.java:226)
    at org.eclipse.persistence.jaxb.JAXBEnumTypeConverter.convertDataValueToObjectValue(JAXBEnumTypeConverter.java:119)
    at org.eclipse.persistence.oxm.mappings.XMLDirectMapping.convertDataValueToObjectValue(XMLDirectMapping.java:528)
    at org.eclipse.persistence.oxm.mappings.XMLDirectMapping.getAttributeValue(XMLDirectMapping.java:296)
    at org.eclipse.persistence.oxm.mappings.XMLDirectMapping.getAttributeValue(XMLDirectMapping.java:1)
    at org.eclipse.persistence.internal.oxm.XMLDirectMappingNodeValue.attribute(XMLDirectMappingNodeValue.java:169)
    at org.eclipse.persistence.internal.oxm.record.UnmarshalRecordImpl.startElement(UnmarshalRecordImpl.java:922)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshallerHandler.startElement(SAXUnmarshallerHandler.java:373)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:380)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:614)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3135)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:880)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:118)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
    at weblogic.xml.jaxp.WebLogicXMLReader.parse(WebLogicXMLReader.java:133)
    at weblogic.xml.jaxp.RegistryXMLReader.parse(RegistryXMLReader.java:173)
    at org.eclipse.persistence.internal.oxm.record.XMLReader.parse(XMLReader.java:243)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:401)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:654)
    at org.eclipse.persistence.internal.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:581)
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:323)
    at org.glassfish.jersey.jaxb.internal.XmlRootElementJaxbProvider.readFrom(XmlRootElementJaxbProvider.java:140)
    at org.glassfish.jersey.jaxb.internal.AbstractRootElementJaxbProvider.readFrom(AbstractRootElementJaxbProvider.java:134)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.invokeReadFrom(ReaderInterceptorExecutor.java:256)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:235)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:155)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1085)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:852)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:786)
    at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:326)
    at org.glassfish.jersey.client.InboundJaxrsResponse$1.call(InboundJaxrsResponse.java:115)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:419)
    at org.glassfish.jersey.client.InboundJaxrsResponse.runInScopeIfPossible(InboundJaxrsResponse.java:267)
    at org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:112)
    at at.myCompany.bookingHubClient.client.RestClient.doWSCallBook(RestClient.java:85)
    at at.myCompany.thirdPartyBookingService.service.impl.hotel.BookResponseServiceImpl.getBookResponse(BookResponseServiceImpl.java:36)
    ... 78 common frames omitted

当然,我发现存在转换问题,但是在tomcat上运行它并且我不使用任何eclipse库。所以意味着任何weblogic库都会覆盖我的。

此行出现异常:

 javax.ws.rs.core.Response bookResponse = response.readEntity(BookResponse.class);

有任何想法让应用再次运行吗?

这是我的weblogic.xml:

<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app
    xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app
        http://xmlns.oracle.com/weblogic/weblogic-web-app/1.4/weblogic-web-app.xsd">
    <wls:context-root>/third-party-booking-service</wls:context-root>
    <wls:container-descriptor>
        <wls:prefer-application-packages>
            <wls:package-name>org.slf4j.*</wls:package-name>
            <wls:package-name>org.springframework.*</wls:package-name>
            <wls:package-name>com.google.common.*</wls:package-name>
            <wls:package-name>javax.annotation.*</wls:package-name>
            <wls:package-name>com.sun.jersey.*</wls:package-name>
            <wls:package-name>org.codehaus.jackson.jaxrs.*</wls:package-name>
            <wls:package-name>org.jboss.logging.*</wls:package-name>
            <wls:package-name>org.hibernate.*</wls:package-name>
            <wls:package-name>com.fasterxml.*</wls:package-name>
            <wls:package-name>org.glassfish.jersey.jaxb.*</wls:package-name>
            <wls:package-name>antlr.*</wls:package-name>
        </wls:prefer-application-packages>
    </wls:container-descriptor>
</wls:weblogic-web-app>

2 个答案:

答案 0 :(得分:1)

是的,您可以在配置时执行此操作,但您需要自己配置数据源,transactionManager和Entitymanager。像这样的东西会做

@Configuration
@EnableJpaRepositories(
    entityManagerFactoryRef = "notDefaultEntityManagerFactory",
    transactionManagerRef = "notDefaultTransactionManager",
    basePackageClasses = notDefaultRepository.class)
public class SomesqlDb{

    @Bean
    public DataSource dataSourcenotDefault() {
        return getDataSource(poolSize, driverClassName, jdbcUrl, userName, password);
    }

    @Bean(name = "notDefaultTransactionManager")
    PlatformTransactionManager notDefaultTransactionManager(EntityManagerFactory notDefaultEntityManagerFactory) {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(notDefaultEntityManagerFactory);
        return txManager;
    }

    @Bean(name = "notDefaultEntityManagerFactory")
    LocalContainerEntityManagerFactoryBean notDefaultEntityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSourcenotDefault());
        em.setPackagesToScan(notDefaultRepository.class.getPackage().getName(), notDefaultBi.class.getPackage().getName());
        em.setPersistenceUnitName("notDefaultDb");

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(false);
        em.setJpaVendorAdapter(vendorAdapter);
        return em;
    }
}  

在这种情况下,持久性单元命名为notDefaultDb。这是通过此行em.setPersistenceUnitName("notDefaultDb")

完成的

编辑:根据来自@ M.Denium的输入,如果您将实体管理器工厂命名为entityManagerFactory(默认),则可以更简单地完成此操作。因此,所有自动配置都可以开箱即用,您最终只能配置实体管理器工厂。

    @Bean
    LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSourcenotDefault());
        em.setPackagesToScan(notDefaultRepository.class.getPackage().getName(), notDefaultBi.class.getPackage().getName());
        em.setPersistenceUnitName("notDefaultDb");

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(false);
        em.setJpaVendorAdapter(vendorAdapter);
        return em;
    }

答案 1 :(得分:0)

感谢@M。 Deinum / @pvpkiran。

万一它对某人有所帮助,以下内容可在WebSphere ND 8.5.5.15上为我工作:

@Configuration
public class JPAConfiguration {

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());

        em.setPackagesToScan(new String[] { "com.example.model" });
        em.setPersistenceUnitName("org.hibernate.jpa.HibernatePersistenceProvider");

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(false);
        em.setJpaVendorAdapter(vendorAdapter);

        return em;
    }

    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName("oracle.jdbc.OracleDriver");
        dataSource.setUrl("jdbc:oracle:thin:@xx.xx.xx.xx:xxxx:db");
        dataSource.setUsername( "username" );
        dataSource.setPassword( "password" );

        return dataSource;
    }
}

我正在使用Spring Boot 2.1.8.RELEASE。