Change composite-id property name with Delegating Reverse Engineering Strategy

I have this table with composite primary key:

CREATE TABLE `arc_test` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `data` datetime DEFAULT NULL,
  `text` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`,`data`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

and this hibernate reverse engineering configuration:

<hibernate-reverse-engineering>  
    <table name="arc_test" catalog="db">
        <primary-key>
            <generator class="native"/>
        </primary-key>       
    </table> 
</hibernate-reverse-engineering> 

and this revenge.strategy

public class CustomReverseEngineeringStrategy extends DelegatingReverseEngineeringStrategy {

    public CustomReverseEngineeringStrategy(ReverseEngineeringStrategy delegate) {
        super(delegate);
    }

    public String columnToPropertyName(TableIdentifier table, String column) {
    	if("arc_test".equals(table.getName()) && "id".equalsIgnoreCase(column))
    		return "compositeId";
        else
    		return super.columnToPropertyName(table, column);
    }
}

Hibernate Tools generated this two class:

public class ArcTest implements java.io.Serializable
{
    private ArcTestId   id;
    private String      text;
    .....
    .....

public class ArcTestId implements java.io.Serializable
{
    private Integer compositeId;
    private Date    data;
        .....
        .....

is it possible with DelegatingReverseEngineeringStrategy class customization change property name of composite-id, like this

public class ArcTest implements java.io.Serializable
{
    private ArcTestId   **compositeId**;
    private String      text;
    .....
    .....

is it possible with reveng strategy?

I don’t understand the question. You already use a CustomReverseEngineeringStrategy and provide a custom name and showed that it does generate what you seem to expect. What’s the issue?

Yes, I use CustomReverseEngineeringStrategy.
But tableToCompositeIdName method, change only the entity class name of composite-id

public String tableToCompositeIdName(TableIdentifier identifier) {
    if("arc_test".equals(identifier.getName()))
    	return "CompositIDClass";
    else
    	return super.tableToCompositeIdName(identifier);
}

this is the outptut

public class ArcTest implements java.io.Serializable
{
    private CompositIDClass   id;
    private String      text;
    .....
    .....

and columnToPropertyName method change only property name inside entity class of composite-id

public String columnToPropertyName(TableIdentifier table, String column) {
    	if("arc_test".equals(table.getName()) && "id".equalsIgnoreCase(column))
    		return "compositeId";
    	else
    		return super.columnToPropertyName(table, column);
    }

this is the outptut

public class ArcTestId implements java.io.Serializable
{
    private Integer compositeId;
    private Date    data;
        .....
        .....

I need change property name of composite-id property inside POJO entity class, like this:

public class ArcTest implements java.io.Serializable
{
    private ArcTestId   compositeId;
    private String      text;
    .....
    .....

Is possible?

If you take a look into the code, you will see how this works and how the columnToPropertyName method is called. See hibernate-tools/PrimaryKeyBinder.java at 6.1.4.Final · hibernate/hibernate-tools · GitHub for details about how the primary key property name is determined.

You just have to override the tableToIdentifierPropertyName method.

Ok I have a problem :grinning:
My project use Java 8 and Hibernate 5.6.
tableToIdentifierPropertyName not exist in DelegatingReverseEngineeringStrategy (5.6 distro)

If I want to use Hibernate 6.1 a need migrate to Java 11, is not fast for my project.

Thanks anyway

Did you look into the source code in your project? AFAICT it is possible: hibernate-tools/ReverseEngineeringStrategy.java at 5.6.12.Final · hibernate/hibernate-tools · GitHub

1 Like