Generate entities from tables with Jbosstools

Asked

Viewed 995 times

0

I created a JPA project and from the resource JPA Tools > Generate Entities from Tables I want to generate the entities related to each table of my database.

I tried to follow a tutorial on the Eclipse website, however it did not work. I believe that the tutorial was done in a version of Jboss Tools different from the one I’m using, since the one I’m using presents a different configuration screen as seen in the image. I use the version of Jboss Tools 4.2.2.Final.

inserir a descrição da imagem aqui

How to properly fill this screen to create my entities?

1 answer

0


"In the middle of the road there was a stone
had a stone in the way
had a stone in the middle of the road there was a stone[...]"
Carlos Drummond de Andrade

Joking aside, I found the reason I couldn’t make the configuration. I have made a brief step by step that will assist anyone with the same problem or those who come across the same.

INSTRUCTIONS

  • Create a JPA project in new > JPA Project;

  • Configure the fields Projectile name with the name of your project; the field Target Runtime with the version of his Rhyme; and in the field Configuration choose the option Basic JPA Configuration and click the Next button;

Configuração do projeto JPA

On the JPA Facet screen (two screens after the JPA Project screen) you must:

  • Click on Add Connection and configure a connection to your database; after that just click the Finish button;

inserir a descrição da imagem aqui

Ready now your JPA project is already configured and ready to generate its entities. Now just start the resource JPA Tools > Generate Entities from Tables right-clicking on the project. The result is figure below. You select the tables and the tool creates the entities already mapped for you. Amazing resource!

inserir a descrição da imagem aqui

The code below shows the code of the Author entity created from the author table and the relations with this table:

package model;

import java.io.Serializable;
import javax.persistence.*;
import java.util.List;


/**
 * The persistent class for the autor database table.
 * 
 */
@Entity
@Table(name="autor")
@NamedQuery(name="Autor.findAll", query="SELECT a FROM Autor a")
public class Autor implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    private String nome;

    //bi-directional many-to-one association to AutorLivro
    @OneToMany(mappedBy="autor")
    private List<AutorLivro> autorLivros;

    public Autor() {
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNome() {
        return this.nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public List<AutorLivro> getAutorLivros() {
        return this.autorLivros;
    }

    public void setAutorLivros(List<AutorLivro> autorLivros) {
        this.autorLivros = autorLivros;
    }

    public AutorLivro addAutorLivro(AutorLivro autorLivro) {
        getAutorLivros().add(autorLivro);
        autorLivro.setAutor(this);

        return autorLivro;
    }

    public AutorLivro removeAutorLivro(AutorLivro autorLivro) {
        getAutorLivros().remove(autorLivro);
        autorLivro.setAutor(null);

        return autorLivro;
    }

}

References

Browser other questions tagged

You are not signed in. Login or sign up in order to post.