简体   繁体   English

Hyperjaxb 3导入的XSD和persistence.xml

[英]Hyperjaxb 3 imported XSD and persistence.xml

This is my first attempt of using Hyperjaxb3. 这是我第一次使用Hyperjaxb3。 I have a snippet of my 2 XSD as shown below 我有一个2 XSD的代码段,如下所示

ContractFullInfo.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 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>

The problem with this it generates two ContractPerson classes as shown below: 与此相关的问题将生成两个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>

and it gives me error during deployment as 并且在部署期间给我错误

 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].

A portion of my Java configuration (which I currently commented) 我的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;
    }

My questions: 我的问题:

  1. Both classes are exactly the same. 这两个类完全相同。 How can I pick one ? 我怎么选一个?
  2. I am using Spring Boot, is there a way to override persistence.xml using Spring Boot java configuration? 我正在使用Spring Boot,是否有一种方法可以使用Spring Boot Java配置覆盖persistence.xml

Your classes are not the same as you have two different complex types in your schema. 您的类不同,因为您的架构中有两种不同的复杂类型。 They also probably reference different PersonRoleType s, but it's hard to say without seeing your complete schemas. 他们可能还引用了不同的PersonRoleType ,但是很难看到完整的模式。

While it's not impossible to map these complex types onto the same Java class, I won't do it. 虽然将这些复杂类型映射到同一Java类不是不可能的,但我不会这样做。 These are distinct types in your schemas, you should keep them distinct in Java as well. 这些是架构中的不同类型,因此在Java中也应使其与众不同。

Since you get a problem with classes having the same local name, the easiest solution would be to rename one of them. 由于您在使用具有相同本地名称的类时遇到问题,因此最简单的解决方案是重命名其中一个。 Use a binding like: 使用如下绑定:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM