AndreaNobili
AndreaNobili

Reputation: 42967

What exactly represent this "Hibernate" class mapping?

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

Upvotes: 0

Views: 118

Answers (2)

Predrag Maric
Predrag Maric

Reputation: 24433

  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.

Upvotes: 2

mikeb
mikeb

Reputation: 11267

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

Upvotes: 1

Related Questions