如何获取类似NetBeans的独立数据库实体类生成工具及命令行使用方法
Hey there! Let's tackle your question by covering both standalone tools that match NetBeans' "Generate Entity Classes from Database" feature, and how to use NetBeans' own functionality via the command line. Here's the breakdown:
If you don't want to rely on NetBeans, these tools work great as standalone solutions:
Hibernate Tools
A popular choice in the Hibernate ecosystem, it supports reverse-engineering databases into JPA/Hibernate entity classes with customizable rules (like field mappings, annotations, and package structures). You can use it via:
- Maven Plugin: Add the
hibernate-tools-maven-pluginto yourpom.xml, configure database connection details and output paths, then run:mvn hibernate:hbm2java - Command Line: Download Hibernate Tools and your JDBC driver, create a
hibernate.cfg.xmlwith database and generation settings, then run:java -cp "hibernate-tools.jar:ojdbc8.jar" org.hibernate.tool.hbm2java.Hbm2Java --config hibernate.cfg.xml
OpenJPA Reverse Mapping Tool
Perfect for JPA-based projects, this tool generates JPA-annotated entities directly from your database. A sample command line usage:
java -jar openjpa-all.jar org.apache.openjpa.jdbc.meta.ReverseMappingTool \ -url jdbc:oracle:thin:@x.x.x.x:1520:orcl \ -user x \ -password x \ -directory entity/ \ -package com.yourproject.entity
Custom Script with JCodeModel
For full control over the generated code, use the JCodeModel library to build a custom tool:
- Use JDBC to fetch database metadata (tables, columns, data types)
- Use JCodeModel to generate Java classes with your preferred annotations (JPA, Lombok, etc.)
This approach lets you tweak every detail of the output to match your project's standards.
NetBeans doesn't have a dedicated standalone CLI for this feature, but you can leverage its internal tools via two methods:
Method 1: Ant Task
NetBeans uses Ant under the hood for this feature, so you can create a standalone build.xml to replicate the workflow:
- Create a
build.xmlfile with the entity generation task:
<project name="EntityGenerator" default="generate-entities" basedir="."> <taskdef name="entitygen" classname="org.netbeans.modules.j2ee.persistence.unitgen.EntityGeneratorTask"> <classpath> <!-- Path to NetBeans persistence module and your JDBC driver --> <pathelement location="/path/to/netbeans/modules/org-netbeans-modules-j2ee-persistence.jar"/> <pathelement location="/path/to/ojdbc8.jar"/> </classpath> </taskdef> <target name="generate-entities"> <entitygen url="jdbc:oracle:thin:@x.x.x.x:1520:orcl" user="x" password="x" destdir="entity/" package="com.yourproject.entity" generateAnnotations="true" generatePrimaryKeyClasses="false" /> </target> </project>
- Run the Ant command:
ant generate-entities
Method 2: Call NetBeans Internal API (Advanced)
If you need more flexibility, write a simple Java program that calls NetBeans' EntityGenerator class:
import org.netbeans.modules.j2ee.persistence.unitgen.EntityGenerator; import java.util.Properties; public class NetBeansEntityGenerator { public static void main(String[] args) { Properties config = new Properties(); config.setProperty("url", "jdbc:oracle:thin:@x.x.x.x:1520:orcl"); config.setProperty("user", "x"); config.setProperty("password", "x"); config.setProperty("destdir", "entity/"); config.setProperty("package", "com.yourproject.entity"); EntityGenerator generator = new EntityGenerator(); try { generator.generateEntities(config); System.out.println("Entities generated successfully!"); } catch (Exception e) { e.printStackTrace(); } } }
Compile and run it with the required classpath:
javac -cp "/path/to/netbeans/modules/org-netbeans-modules-j2ee-persistence.jar:/path/to/ojdbc8.jar" NetBeansEntityGenerator.java java -cp ".:/path/to/netbeans/modules/org-netbeans-modules-j2ee-persistence.jar:/path/to/ojdbc8.jar" NetBeansEntityGenerator
内容的提问来源于stack exchange,提问作者Yunus Emrah Dursun

