0

Can someone help me how should I join those three tables using JPA?

db model

I already did 2 of 3 entities but please let me know if are ok:

@Entity
public class Pacienti {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String nume;
    private String prenume;

//setters & getters

}


@Entity
public class Chestionare {

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

    @Id
    @Column(name = "id_intrebare")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int idIntrebare;

    private String intrebare;
//setters & getters
}

As I promise I come back after I'm generating entities automatically. Unfortunately now I have another problem. Now I have the entity:

@Entity
@Table(name = "pacienti")
@NamedQuery(name = "Pacienti.findAll", query = "SELECT p FROM Pacienti p")
public class Pacienti implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(unique = true, nullable = false)
    private int id;

    @Column(nullable = false, length = 20)
    private String nume;

    @Column(nullable = false, length = 20)
    private String prenume;

    // bi-directional many-to-one association to Consultatii
    @OneToMany(mappedBy = "pacienti")
    private List<Consultatii> consultatiis;

    // bi-directional many-to-one association to DetaliiPacient
    @OneToMany(mappedBy = "pacienti")
    private List<DetaliiPacient> detaliiPacients;

    // bi-directional many-to-one association to Doctori
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_doctor", nullable = false)
    private Doctori doctori;

    // bi-directional many-to-one association to RaspunsChestionar
    @OneToMany(mappedBy = "pacienti")
    private List<RaspunsChestionar> raspunsChestionars;

    public Pacienti() {
    }

//setters and getters
}

But when I do :

Query queryResult = sessionFactory.getCurrentSession().createQuery("from Pacienti");

I'm getting:

Pacienti is not mapped [from Pacienti] Error.

Can someone tell me why? I also tried "pacienti is not mapped [from pacienti]" but same result

Thank you!

Aditzu
  • 696
  • 1
  • 14
  • 25

2 Answers2

1

I would recommend you to use the jpa tools/plugins available with the IDEs which will auto generate these jpa entities for you using the database tables rather than manually creating these. And they will take care of setting the relationship b/w different entities(db tables) in the auto generation process itself. If you are Eclipse you can achieve this.

mhasan
  • 3,703
  • 1
  • 18
  • 37
  • I'm using Eclipse indeed. I'll try a plugin as you said and I'll come back for saying what I achieved. Thank you! – Aditzu Sep 18 '16 at 11:03
  • You may look at this post might help in generation of jpa entities. http://stackoverflow.com/questions/5833329/generate-jpa-2-entities-from-existing-database – mhasan Sep 18 '16 at 11:58
  • Event if this is not the final solution, got me on the right track so I will accept it as a solution. Thanks! – Aditzu Sep 19 '16 at 07:59
0

The problem is bcz there is no query with the name "from pacienti" in place of that pass the query name "Pacienti.findAll" in your createQuery method. Plz let ne know once you try this, if you face any problem

mhasan
  • 3,703
  • 1
  • 18
  • 37
  • Unfortunately I'm getting "ERROR: line 1:1: unexpected token: Pacienti" – Aditzu Sep 19 '16 at 07:12
  • Moreover if I'm trying to save something on that entity I'm getting :Unknown entity: com.filemanager.utils.transporters.generatedEntities.Pacienti – Aditzu Sep 19 '16 at 07:35
  • It seems that I forgot to set the package scan for entities: LocalSessionFactoryBean factory = new LocalSessionFactoryBean(); factory.setDataSource(dataSource()); factory.setPackagesToScan("com.filemanager.utils.transporters.generatedEntities"); – Aditzu Sep 19 '16 at 07:50
  • I succeded to make an insert but findAll() still not ok :( – Aditzu Sep 19 '16 at 07:53
  • After I scanned the entities ok, Query queryResult = sessionFactory.getCurrentSession().createQuery("from Pacienti"); worked just fine – Aditzu Sep 19 '16 at 07:54
  • thats grt8 @Aditzu – mhasan Sep 19 '16 at 07:57
  • Thank a lot mhasan – Aditzu Sep 19 '16 at 07:58