gpt4 book ai didi

hibernate - 如何将持久性单元的 JPA EntityManagerFactory 设置为不为 'default'

转载 作者:行者123 更新时间:2023-12-03 08:16:35 24 4
gpt4 key购买 nike

通常Spring-boot的所有默认配置都是完全没问题的。但现在我需要将我的应用程序部署到 Weblogic 12.2 服务器,我面临着一些关于持久性的问题。

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

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) 或真的 hibernate 。所以我想在我的 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>

最佳答案

是的,你可以在配置时进行,但你需要自己配置你的数据源、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(default),这可以更简单地完成。这样所有自动配置都可以开箱即用,您最终只需要配置实体管理器工厂。

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

关于hibernate - 如何将持久性单元的 JPA EntityManagerFactory 设置为不为 'default',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44127381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com