JPA和Hibernate注释之间的关系 [英] Relation between JPA and Hibernate annotation

查看:87
本文介绍了JPA和Hibernate注释之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring世界中很新,我正在研究如何集成Hibernate和Spring框架。

我对Hibernate注释和JPA注释之间的关系有些厌倦。



Whene我有类似于以下实体类的内容:

  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 =人)
public class Person {

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

私人字符串名字;

私人字符串姓氏;

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

我不理解的是Hibernate注释和JPA注释之间的关系
$ b

注释 @Entity @Table(name =person) ,在前面的例子中使用的 @GeneratedValue(strategy = GenerationType.AUTO) id 是简单的JPA注释还是Hibernate的特定注释?



如果我使用JPA批注将数据持久化到数据库,那么给我什么Hibernate?



Tnx



Andrea

解决方案

答案是混合的。

正如其他人指出的那样,JPA是Hibernate提供实现的规范。您使用JPA批注/ API,并且通过在类路径中包含Hibernate jar,Hibernate将提供实际的逻辑。

另外,Hibernate提供了一个与JPA无关的API。你也可以使用它;主要区别在于:如果您使用JPA API,稍后您可以用其他JPA实现替换Hibernate(vg EclipseLink)如果直接使用Hibernate API,则不需要对程序进行任何更改
  • 优点可能是您可以使用Hibernate定义的功能,但这些功能不属于JPA标准的一部分,可能对您有用。




  • 无论如何,您应该完全避免将JPA与Hibernate API混合。作为初学者,我建议你坚持使用JPA。



    为确保您使用的是JPA,只包含来自 java的类/注释。持久性。不要包含 org.hibernate 中的任何内容(或者,如果您只想使用Hibernate,请做相反的事情)。


    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

    解决方案

    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).

    这篇关于JPA和Hibernate注释之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆