How to set JPA EntityManagerFactory for persistence unit NOT to 'default'

半城伤御伤魂 提交于 2020-01-04 04:33:06

问题


Noramlly all the default configurations of Spring-boot are totally fine. But now I need to deploy my application to a Weblogic 12.2 Server and I am facing some issues regarding persistence.

I just wondering how to set the JPA EntityManagerFactory for persistence unit NOT to 'default'. Because by startup of the application I can see those logs:

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

So I am not sure if weblogic is using the default persistence unit (eclipselink) or really hibernate. So I was thinking to set the persistence unit explicity anywhere in my spring-boot application.

Is there any way to set hibernate as default unit explicit?

EDIT:

I was asking this before because I get an exception by runtime. The org.eclipse.persistence libraries are used for unmarshalling 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

Sure I see there is a conversion issue, but on tomcat its running and I dont use any eclipse libraries. So means any library of weblogic is overriding mine.

The exception occours at this line:

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

Any idea to get the app running again?

This is my 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>

回答1:


Yes you can do it when you configure, but you need to configure your datasource, transactionManager and Entitymanager yourself. Something like this will do

@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;
    }
}  

In this case persistance unit is named as notDefaultDb. This is done by this line em.setPersistenceUnitName("notDefaultDb")

EDIT : Based on inputs from @M.Denium, this can be done even simpler if you name your entity manager factory as entityManagerFactory(default) . So that all the Auto configuration works out of the box and you would end up configuring only entity manager factory.

    @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;
    }



回答2:


Thanks @M. Deinum / @pvpkiran.

In case it helps someone, the following works for me on 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;
    }
}

I am using Spring boot 2.1.8.RELEASE.



来源:https://stackoverflow.com/questions/44127381/how-to-set-jpa-entitymanagerfactory-for-persistence-unit-not-to-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!