Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am quite new in Spring world and I am studying how to integrate Hibernate and Spring framework

i have some dount about the relation beetween Hibernate annotation and JPA annotation.

Whene I have something like the following entity class:

package org.andrea.myexample.HibernateOnSpring.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


/* Classe che rappresenta un oggetto da persistere sul database
 * 
 */
@Entity
@Table(name="person")
public class Person {

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

    private String firstname;

    private String lastname;

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }

    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;
    }
}

The thing that I am not understanding is what is the relationship between Hibernate annotation and JPA annotation

The annotation @Entity, @Table(name="person"), @GeneratedValue(strategy=GenerationType.AUTO) and id using in the previous example are simple JPA annotation or specific annotation for Hibernate?

if I'm using JPA annotations to persist the data to the database, what's giving me Hibernate?

Tnx

Andrea

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
289 views
Welcome To Ask or Share your Answers For Others

1 Answer

The answer is mixed.

As others have pointed out, JPA is an specification an Hibernate provides an implementation. You use JPA annotations/API and, by including Hibernate jars in your classpath, Hibernate will provide the actual logic.

Additionally, Hibernate offers an API that is unrelated to JPA. You can use that as well; the main difference is:

  • if you use the JPA API, you may later replace Hibernate by other JPA implementation (v.g. EclipseLink) and you will need no changes to your program

  • if you use directly Hibernate API, you have no implementation alternatives. The advantage may be that you can use features that Hibernate has defined but that are not part of JPA standard, which might be usefult to you.

At any rate, what you should completely avoid is mixing JPA with the Hibernate API. As a beginner, I would advice you to stick to JPA.

To ensure that you are using JPA, only include classes/annotations from java.persistence. Do not include anything from org.hibernate (or, if you want just to use Hibernate, do just the opposite).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...