1

I am using Netbeans 12.0, and to create my Entity classes for my DB tables, I use Netbeans Entity Classes from Database. It works great other than the fact it defaults all my date columns to the pre Java 8 Date classes, and I recently discovered removing the @Temporal annotation from my class variables (correspond with columns in my tables) with LocalDate does not cause issues.

This is what Netbeans creates:

@Column(name = "delivery_date")
@Temporal(TemporalType.DATE)
private Date deliveryDate;

Here is my updated version that works:

@Column(name = "delivery_date")
private LocalDate deliveryDate;

Is it possible to make Netbeans use the LocalDate class when creating my entity classes from my database?

I have many tables which have corresponding entities in my Java app, while it is theoretically feasible for me to go through each one and replace the columns with dates, it does not sound like fun especially for future entities.

My project is Java 8, using JPA 2.2 and EclipseLink 2.7.7.

cela
  • 2,059
  • 3
  • 19
  • 37

1 Answers1

2

I'm afraid that's not possible, when using regular Netbeans version.

Here is snippet from class JavaPersistenceGenerator responsible for that mapping:

String getMemberType(EntityMember m) {
    String memberType = m.getMemberType();
    if ("java.sql.Date".equals(memberType)) { //NOI18N
        memberType = "java.util.Date";
    } else if ("java.sql.Time".equals(memberType)) { //NOI18N
        memberType = "java.util.Date";
    } else if ("java.sql.Timestamp".equals(memberType)) { //NOI18N
        memberType = "java.util.Date";
    }
    return memberType;
}

As you can see java.util.Date is hard-coded there and cannot be configured.

You can checkout Netbeans sources from GitHub, change java.util.Date to java.time.LocalDate in class:

org.netbeans.modules.j2ee.persistence.wizard.fromdb.JavaPersistenceGenerator

Rebuild Netbeans sources and replace file:

<<NETBEANS_INSTALLATION_FOLDER>>/java/modules/org-netbeans-modules-j2ee-persistence.jar

With corrected version from:

<<NETBEANS_SOURCES_FOLDER>>/nbbuild/netbeans/java/modules

That will lead to the desired result.

cela
  • 2,059
  • 3
  • 19
  • 37
Alexandra Dudkina
  • 2,945
  • 3
  • 5
  • 21