Netbeans 12.0 – 从数据库生成实体类使用了 Java Date 类,而不是 LocalDate 类。

huangapple go评论78阅读模式
英文:

Netbeans 12.0 - Generating Entity Classes from DB uses Java Date class instead of LocalDate

问题

以下是翻译好的内容:

我正在使用 Netbeans 12.0,在创建我的数据库表的实体类时,我使用 Netbeans 的“从数据库创建实体类”功能。除了一个问题,它将所有的 date 列默认设置为 Java 8 之前的 Date 类,我最近发现,将类变量(与表中的列相对应)中的 @Temporal 注解删除,并改为使用 LocalDate 并不会引发问题。

这是 Netbeans 创建的代码:

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

这是我更新后可行的版本:

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

在从数据库创建实体类时,是否有可能让 Netbeans 使用 LocalDate 类呢?

我有许多表在我的 Java 应用中有相应的实体,虽然从理论上讲,我可以逐个表格进行操作,将日期列替换为 LocalDate,但这听起来并不是一件有趣的事情,特别是对于未来的实体。

我的项目使用的是 Java 8,使用 JPA 2.2 和 EclipseLink 2.7.7。

英文:

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.

答案1

得分: 3

抱歉,这是要翻译的内容:

"我怕在使用常规的Netbeans版本时是不可能的。

这是负责该映射的JavaPersistenceGenerator类的片段:

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;
}

正如您所看到的,java.util.Date 在这里是硬编码的,无法配置。

您可以从GitHub检出Netbeans源代码,然后在类中将java.util.Date 更改为java.time.LocalDate

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

重新构建Netbeans源代码,并用来自以下位置的已校正版本替换文件:

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

这将导致所需的结果。

英文:

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 (&quot;java.sql.Date&quot;.equals(memberType)) { //NOI18N
        memberType = &quot;java.util.Date&quot;;
    } else if (&quot;java.sql.Time&quot;.equals(memberType)) { //NOI18N
        memberType = &quot;java.util.Date&quot;;
    } else if (&quot;java.sql.Timestamp&quot;.equals(memberType)) { //NOI18N
        memberType = &quot;java.util.Date&quot;;
    }
    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:

&lt;&lt;NETBEANS_INSTALLATION_FOLDER&gt;&gt;/java/modules/org-netbeans-modules-j2ee-persistence.jar

With corrected version from:

&lt;&lt;NETBEANS_SOURCES_FOLDER&gt;&gt;/nbbuild/netbeans/java/modules

That will lead to the desired result.

huangapple
  • 本文由 发表于 2020年9月30日 00:44:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64124066.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定