3

I am using Spring boot 1.4.0, Spring data for repository layer and Hibernate for JPA.

When I use org.springframework.data.annotation.Transientannotation on one of the entity field that I do not want to persist, it does not work.
When I use javax.persistence.Transient, it works as expected(every annotation is from javax.persistence package).

I have done a lot of readings of the documents but could not find out why. Please help me understand the difference, thank you.

Update

From the comment and answer below, I changed both @Id and @transient from javax.persistence package to org.springframework.data.annotation, it still produces error that says:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.xxx.domain.entities.PriceScheduleEntity

I guess I need to change all the annotations including @Column, @Entity, etc.. to the same vendor/provider/package to make it work. JPA and spring's annotations cannot mixed? I'll try and update later.

Community
  • 1
  • 1
Minjun Yu
  • 3,497
  • 4
  • 24
  • 39
  • You mean when you use the STANDARDISED PERSISTENCE annotation for persistence, it works, and when you use the proprietary Spring annotation it doesn't? The clue is in the package. One is designed to do that. "Spring Data JPA" != JPA API – Neil Stockton Sep 21 '16 at 05:51
  • Thank you I updated my question, I'll try changing all the annotations from Spring and update later. – Minjun Yu Sep 21 '16 at 06:12

2 Answers2

4

Springs @Transient is only relevant when Spring Data does the mapping as in Spring Data MongoDb for example. With Spring Data JPA you should use the javax.persistence.Transient annotation, since JPA (or it's implementation) does the mapping and those will obviously ignore Spring annotations.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
1

If you have an attribute in your class that you did not intend to have persisted. You must mark any such non-persistent fields using the @Transient annotation or <transient> element.

As per Documentation of @Transient in javax.persistence.Transient-

This annotation specifies that the property or field is not persistent. It is used to annotate a property or field of an entity class, mapped superclass, or embeddable class.

javax.persistence.transient will make sure that Hibernate will ignore that particular field from saving it into the database.

try to change your @Id annotation on entity from javax.persistence.Id to org.springframework.data.annotation.Id and see. Hope it will work.

Sai prateek
  • 11,842
  • 9
  • 51
  • 66
  • Changing @Id from javax.persistence.transient to spring one alone still produces error. My guess is that I should use annotations from either spring or jpa but never both? I'll try that later. – Minjun Yu Sep 21 '16 at 06:05