1

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 ?

priyas
  • 415
  • 1
  • 8
  • 29
  • Do you want to change naming strategy for all your foreign keys or just for this particular one? – zbig Dec 31 '14 at 10:31

1 Answers1

0

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.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • Thanks a lot , I am able to customize the foreign key column name. Is it also possible that I write one rule at only one place( my foreign key name to be similar as my schema element name ) . And It will be true for all the foreign keys generation? – priyas Jan 05 '15 at 08:56
  • @priyas If you want to customize FK names generation in general, then it's really a custom naming strategy. You'll probably need to override [this method](https://github.com/highsource/hyperjaxb3/blob/master/ejb/plugin/src/main/java/org/jvnet/hyperjaxb3/ejb/strategy/naming/impl/DefaultNaming.java#L238-L252). – lexicore Jan 05 '15 at 09:23