简体   繁体   English

重命名使用自定义命名策略生成的生成的外键列

[英]Rename the generated foreign key column generated using custom Naming Strategy

I want to change the name of the foreign key column generated by my Schema. 我想更改由我的架构生成的外键列的名称。 Below is the schema configuration that I am using : 以下是我正在使用的架构配置:

    <xs:complexType name="ActivityFact">
        <xs:sequence>
                    <!-- Other Element Declaration -->
        </xs:sequence>
        <xs:attribute name="id" type="xs:long" />
    </xs:complexType>

When I run the this configuration , I got 2 tables : 1. ActivityDim 2. ActivityFact ( ActivityDim Id as a foreign key with name activityDim_ActivityFact_Id) 当我运行此配置时,我得到2个表:1. ActivityDim 2. ActivityFact(作为名称为ActivityDim_ActivityFact_Id的外键的ActivityDim ID)

I want to change the above generated name to the schema element name which is actvitiyDim in this case. 我想将上面生成的名称更改为在这种情况下为actvitiyDim的架构元素名称。 I am not sure how to use the custom naming Strategy. 我不确定如何使用自定义命名策略。 I have tried to override foreignKeyColumnName method , but didn't work 我试图覆盖foreignKeyColumnName方法,但是没有用

public class ForeignKeyNamingStrategy extends ImprovedNamingStrategy {

    private final static Logger logger = LoggerFactory.getLogger(ForeignKeyNamingStrategy.class);

    @Override
    public String foreignKeyColumnName(
            String propertyName, String propertyEntityName, String propertyTableName, String referencedColumnName
    ) {
        return  propertyName; 
    }

I have also given this class reference in my persistence.xml 我在persistence.xml中也提供了此类参考

<persistence version="2.0"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd
http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:orm="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <persistence-unit name="reportingData">
        <!-- These properties are defined in persistence-model/src/main/resources/persistence.xml. 
            HyperJAXB3 merges them into its generated and final persistence.xml -->
        <properties>
            <property name="hibernate.ejb.naming_strategy"
                value="com.namingStrategy.ForeignKeyNamingStrategy" /></properties>
    </persistence-unit>
</persistence>

I am new to Hibernate , my understanding could be a bit disconnected. 我是Hibernate的新手,我的理解可能会有点疏远。 Can someone please suggest ? 有人可以建议吗?

Disclaimer: I'm the author of Hyperjaxb. 免责声明:我是Hyperjaxb的作者。

This is a preliminary answer, we'll probably need to refine it later on. 这是一个初步的答案,稍后我们可能需要对其进行完善。

First of all, please show the full schema fragment with ActivityDim and ActivityFact as well as what's generated. 首先,请显示带有ActivityDimActivityFact的完整架构片段以及生成的内容。 Do you have a many-to-one or many-to-many or one-to-many relationship here? 您在这里有many-to-onemany-to-manyone-to-many关系吗?

Next, check the customization guide . 接下来,查看自定义指南 As far as I understand your problem, you probably need to customize the name of the foreign key column. 据我了解您的问题,您可能需要自定义外键列的名称。 This should be possible OOTB without implementing a custom namin strategy. 如果不执行自定义的namin策略,这可能是OOTB。 Assuming you have a one-to-many relationship here, this will be somewhere here . 假设您在这里有one-to-many关系,那么这将在这里 This is the relevant part of the ORM schema which is used for customizations. 这是用于定制的ORM架构相关部分 I think you need to customize the name of the join-column here so my guess would be something like: 我认为您需要在此处自定义join-column的名称,所以我的猜测将是这样的:

        <hj:one-to-many>
            <orm:join-column name="actvitiyDim"/>
        </hj:one-to-many>

Here's an example from one of the test projects : 这是其中一个测试项目的示例

        <xs:element name="one-to-many-join-column" type="test:two" minOccurs="0" maxOccurs="unbounded">
            <xs:annotation>
                <xs:appinfo>
                    <hj:one-to-many>
                        <orm:join-column name="O2MJC_ONE_ID"/>
                    </hj:one-to-many>
                </xs:appinfo>
            </xs:annotation>
        </xs:element>

Probably quite close to your case. 可能非常接近您的情况。

Now, naming strategies. 现在,命名策略。

Naming strategy has nothing to do with Hibernate or JPA. 命名策略与Hibernate或JPA无关。 Naming strategy is a internal component which Hyperjaxb3 uses during schema compilation to generate names of tables, columns etc. So it's actually very low-level. 命名策略是Hyperjaxb3在架构编译期间使用的内部组件,用于生成表,列等的名称。因此,它实际上是非常底层的。

If you for some reason want a different naming, you can write and configure your own implementation of the naming strategy. 如果出于某种原因想要不同的命名,则可以编写和配置自己的命名策略实现。 See this test project for example. 例如,请参见此测试项目 But it's no longer customization , it's already extending Hyperjaxb. 但是它不再是自定义的 ,它已经扩展了 Hyperjaxb。 You would basically rewrite a part of Hyperjaxb. 您基本上将重写 Hyperjaxb的一部分。 For instance you could do this if you want to change how all of the FK names are generated by default. 例如,如果要更改默认情况下生成所有 FK名称的方式,可以执行此操作。 I'm not sure this is what you want here. 我不确定这就是您想要的东西。

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

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