Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

java - Difference between JPA Entity and Hibernate Entity

When I annotate a class with @Entity and try to resolve the dependencies, I get to choose the package between two different packages, javax.persistence.Entity and org.hibernate.annotations.Entity

The javax package is JPA's entity-annotation, but why is there a hibernate entity-annotation and difference does it have with JPA's annotation? Is it just an extension to allow more attributes to be defined?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

org.hibernate.annotations.Entity has some extra attributes that javax.persistence.Entity has not standarized. The extra features will only work if using hibernate's AnnotationConfiguration directly or if hibernate is the JPA provider.

from the FAQ: edit: new link the specific question: edit: new link the answer:

I use @org.hibernate.annotations.Entity and get an Unknown entity exception

Always import @javax.persistence.Entity

@org.hibernate.annotations.Entity completes @javax.persistence.Entity but is not a replacement

For instance, there is an attribute called optimisticLock, which tells hibernate whether to use the standard version column or to compare all columns when updating. This behavior is not in the JPA spec, so in order to configure it, you must use hibernate specific extension found in their own annotation.

Like this:

@Entity
@org.hibernate.annotations.Entity(optimisticLock=OptimisticLockType.ALL)
public class MyEntity implements Serializable {
...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...