gpt4 book ai didi

java - Hyperjaxb 3导入XSD和persistence.xml

转载 作者:行者123 更新时间:2023-12-02 10:28:49 25 4
gpt4 key购买 nike

这是我第一次尝试使用Hyperjaxb3。我有 2 XSD 的片段,如下所示

ContractFullInfo.xsd

<xsd:import namespace="http://homecredit.net/homerselect/common/v1" schemaLocation="Common.xsd"/>

<xsd:element name = "ContractFullInfoRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="systemEvent" type="common:ContractSystemEventType"/>
<xsd:element name="data" type="ContractFullInfo"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

<xsd:complexType name="ContractPerson">
<xsd:sequence>
<xsd:element name="cuid" type="xsd:long"/>
<xsd:element name="personRole" type="PersonRoleType"/>
</xsd:sequence>
</xsd:complexType>

Common.xsd

<xsd:complexType name="ContractPerson">
<xsd:sequence>
<xsd:element name="cuid" type="xsd:long"/>
<xsd:element name="personRole" type="PersonRoleType"/>
</xsd:sequence>
</xsd:complexType>

问题是它会生成两个 ContractPerson 类,如下所示:

        <class>net.homecredit.homerselect.common.v1.ContractPerson</class> <==
<class>net.homecredit.homerselect.common.v1.MoneyDto</class>
<class>net.homecredit.homerselect.contract.v3.BankAccount</class>
<class>net.homecredit.homerselect.contract.v3.ClosedEndParameter</class>
<class>net.homecredit.homerselect.contract.v3.ContractBase</class>
<class>net.homecredit.homerselect.contract.v3.ContractCommodity</class>
<class>net.homecredit.homerselect.contract.v3.ContractDocument</class>
<class>net.homecredit.homerselect.contract.v3.ContractEvent</class>
<class>net.homecredit.homerselect.contract.v3.ContractFullInfo</class>
<class>net.homecredit.homerselect.contract.v3.ContractFullInfoRequest</class>
<class>net.homecredit.homerselect.contract.v3.ContractParameter</class>
<class>net.homecredit.homerselect.contract.v3.ContractPerson</class> <==
<class>net.homecredit.homerselect.contract.v3.ContractService</class>
<class>net.homecredit.homerselect.contract.v3.RefinancedContract</class>
<class>net.homecredit.homerselect.contract.v3.RevolvingParameter</class>

它在部署期间给我错误

 Entity name must be unique in a persistence unit. Entity name [ContractPerson] is used for the entity classes [net.homecredit.homerselect.common.v1.ContractPerson] and [net.homecredit.homerselect.contract.v3.ContractPerson].

我的 Java 配置的一部分(我当前已评论)

  @Bean
public DataSource dataSource() throws NamingException {
return (DataSource) new JndiTemplate().lookup(env.getProperty("spring.datasource.jndi-name"));
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException {
LocalContainerEntityManagerFactoryBean em
= new LocalContainerEntityManagerFactoryBean();
em.setPackagesToScan(new String[]{"net.homecredit.homerselect.contract.v3"});
em.setPersistenceUnitName("net.homecredit.homerselect.common.v1:net.homecredit.homerselect.contract.v3");
em.setJtaDataSource(dataSource());

Properties properties = new Properties();
properties.setProperty("hibernate.dialect", env.getProperty("spring.jpa.properties.hibernate.dialect"));
properties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("spring.jpa.hibernate.ddl-auto"));
em.setJpaProperties(properties);

return em;
}

我的问题:

  1. 两个类完全相同。我该如何选择?
  2. 我正在使用 Spring Boot,有没有办法使用 Spring Boot java 配置覆盖 persistence.xml

最佳答案

您的类不同,因为您的架构中有两种不同的复杂类型。它们还可能引用不同的 PersonRoleType,但如果没有看到完整的架构,就很难说。

虽然将这些复杂类型映射到同一个 Java 类并非不可能,但我不会这样做。这些在您的模式中是不同的类型,您也应该在 Java 中保持它们不同。

由于您遇到具有相同本地名称的类的问题,最简单的解决方案是重命名其中一个。使用如下绑定(bind):

<jaxb:bindings
version="1.0"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:hj="http://hyperjaxb3.jvnet.org/ejb/schemas/customizations"
xmlns:orm="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc hj orm">

<jaxb:bindings schemaLocation="Common.xsd" node="/xs:schema">
<jaxb:bindings node="xs:complexType[@name='ContractPerson']">
<jaxb:class name="CommonContractPerson"/>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>

关于java - Hyperjaxb 3导入XSD和persistence.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53723733/

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