0

I am working on a Java web application that I think use Hibernate and I am not so into Hibernate so I have the following doubt:

I have a model class named ReaDichiarazioneIntento that map a database table named REA_DICHIARAZIONE_INTENTO, something like this:

@javax.persistence.IdClass(it.enel.wearea.entity.ReaDichiarazioneIntentoPK.class)
@javax.persistence.Table(name = "REA_DICHIARAZIONE_INTENTO", schema = "EDIWEA")
@Entity
public class ReaDichiarazioneIntento implements Cloneable {

    private Integer idDichiarazione;

    @javax.persistence.Column(name = "ID_DICHIARAZIONE")
    @Id
    public Integer getIdDichiarazione() {
        return idDichiarazione;
    }

    public void setIdDichiarazione(Integer idDichiarazione) {
        this.idDichiarazione = idDichiarazione;
    }

    private Integer idCliente;

    @javax.persistence.Column(name = "ID_CLIENTE")
    @Basic
    public Integer getIdCliente() {
        return idCliente;
    }

    public void setIdCliente(Integer idCliente) {
        this.idCliente = idCliente;
    }

    ...................................................................
    ...................................................................
    ...................................................................
    SOME OTHER FIELDS AND RELATED GETTER AND SETTER METHODS
    ...................................................................
    ...................................................................
    ...................................................................

}

Ok I have some doubts about this class. My doubt are:

1) Is it using Hibernate for mapping the class to the database table? Or what? I know that to map a database table to a class I have to do something like:

@Entity
@Table(name = "REA_DICHIARAZIONE_INTENTO")

Why in this project do:

@javax.persistence.IdClass(it.enel.wearea.entity.ReaDichiarazioneIntentoPK.class)
@javax.persistence.Table(name = "REA_DICHIARAZIONE_INTENTO", schema = "EDIWEA")
@Entity

What is the difference between the @Table(name = "REA_DICHIARAZIONE_INTENTO") annotation and the @javax.persistence.Table(name = "REA_DICHIARAZIONE_INTENTO", schema = "EDIWEA") annotation (used in my project)?

2) The second doubt is related to this annotation:

@javax.persistence.IdClass(it.enel.wearea.entity.ReaDichiarazioneIntentoPK.class)

What exactly means?

3) The last doubt is related to the mapping between a class field to a table column on the DB. Why is it done only on the getter method and not directly on the field name?

Tnx

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

2 Answers2

2
  1. It is using JPA annotations, and Hibernate is a JPA implementation. JPA by itself is just a set of interfaces/annotations, while JPA implementation (like Hibernate) provides meat around those interfaces/annotations. There is no difference between the two annotations, other than specified schema. Hibernate also has its own @Table annotation but it is used for additional information supplied by JPA'a @Table annotation
  2. @IdClass means that the complex primary key is used for this entity

Specifies a composite primary key class that is mapped to multiple fields or properties of the entity.

  1. You can annotate fields or properties (getters), it's up to you. But, @Id mapping dictates what is valid, meaning if you put @Id on field then you must put all other mappings on fields also, and vice versa.
Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
1

This is using JPA, looks like, not hibernate. Here is the difference according to SO and here is another link

Community
  • 1
  • 1
mikeb
  • 10,578
  • 7
  • 62
  • 120
  • mmm...I know that JPA is only a SPECIFICATION (noi an implementation) and that Hibernate is one of the JPA IMPLEMENTATION. From what I know I have however use a JPA implementation and I can't use JPA alone, or am I missing something? – AndreaNobili Jun 24 '15 at 14:53
  • 1
    @AndreaNobili you are correct. So in essence the answer to question 1) is yes, Hibernate is doing the mapping. But according to the rules of the standardized JPA specification; as such for further understanding you really should study JPA too. – Gimby Jun 24 '15 at 15:11
  • @Gimby Ok...so let me understand better...in practice you are sayng to me that in this example Hibernate is under the hood and do the persinstence work but the mapping is specified using the JPA annotation and not the Hibernate annotation? – AndreaNobili Jun 24 '15 at 15:15
  • 1
    @AndreaNobili you are correct again. You will find this kind of API/implementation split quite a lot on the Java platform. Oracle defines the standard API, different third party vendors are free to provide implementations for it in their own way and with their own levels of quality assurance. – Gimby Jun 24 '15 at 15:27
  • If you want to make your aplication portable avoid hibernate specific features/anotations. Also EclipseLink is the reference implementation for JPA but it also has some specific features not included in the jpa specification but it is more close than hibernate to the standard – Ismael_Diaz Jun 25 '15 at 15:13