convert SQL table to java bean class [closed]

孤者浪人 提交于 2019-12-18 18:07:36

问题


I'm working in web application using a huge number of SQL tables. I need to create for each table a java bean class. I'm searching for a tool that can convert SQL table to a java bean class. It will help to save time.

Here is an example:

studentTable (studentId, firstname, lastname, yearLevel)

-->

public class Student {
    @Id
    @Column
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int studentId;
    @Column
    private String firstname;
    @Column
    private String lastname;
    @Column
    private int yearLevel;

    public Student(){}
    public Student(int studentId, String firstname, String lastname,
            int yearLevel) {
        super();
        this.studentId = studentId;
        this.firstname = firstname;
        this.lastname = lastname;
        this.yearLevel = yearLevel;
    }
    public int getStudentId() {
        return studentId;
    }
    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    public int getYearLevel() {
        return yearLevel;
    }
    public void setYearLevel(int yearLevel) {
        this.yearLevel = yearLevel;
    }
}

Do you have any idea about such tool.


回答1:


Use Hibernate.

It allows you to generate model-classes for your database tables.

The "function" is called: Hibernate mapping files and POJO'S from database, also use Hibernate Reverse Engineering Wizard.

I use Netbeans and Hibernate to generate all these models.




回答2:


Eclipse has such functionality, but I'm not sure that it works correctly for all cases and that it properly handles relations between tables. In any case take a look at this link(Creating and Modifying Entities section), it might help.



来源:https://stackoverflow.com/questions/16523408/convert-sql-table-to-java-bean-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!