简体   繁体   中英

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)

I want to change the above generated name to the schema element name which is actvitiyDim in this case. I am not sure how to use the custom naming Strategy. I have tried to override foreignKeyColumnName method , but didn't work

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 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. Can someone please suggest ?

Disclaimer: I'm the author of 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. Do you have a many-to-one or many-to-many or one-to-many relationship here?

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. Assuming you have a one-to-many relationship here, this will be somewhere here . This is the relevant part of the ORM schema which is used for customizations. I think you need to customize the name of the join-column here so my guess would be something like:

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

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. You would basically rewrite a part of Hyperjaxb. For instance you could do this if you want to change how all of the FK names are generated by default. I'm not sure this is what you want here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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