This week's book giveaway is in the Web Services forum.
We're giving away four copies of Microservices in Action and have Morgan Bruce & Paulo A. Pereira on-line!
See this thread for details.
Win a copy of Microservices in Action this week in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

This class has a composite primary key. It must use an ID class.  RSS feed

 
Ranch Hand
Posts: 183
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have a class that is annotated by several @Id, it makes me an error on the @Entity of this class.

How can I remove this error?


 
Bartender
Posts: 9498
184
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A quick google search implies you need to make a separate class for the compound key and embed it:
 
Philippe Ponceblanc
Ranch Hand
Posts: 183
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks

Just one problem are in this code,
the @Embeddable = i don't know and generate error == The Java class for mapped type "flights.RouteId" is final

 
Stephan van Hulst
Bartender
Posts: 9498
184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not super familiar with JPA, but it seems that they need to create proxy classes to function properly. That means classes need to be non-final. I write final by default, but apparently that won't work in this situation.
 
Philippe Ponceblanc
Ranch Hand
Posts: 183
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
it seems to me that the solution is in the following link:

https://translate.googleusercontent.com/translate_c?depth=1&hl=fr&prev=search&rurl=translate.google.fr&sl=en&sp=nmt4&u=https://stackoverflow.com/questions/12106124/eclipse-error-on-mapping-with-embeddedid/26540478&xid=17259,15700022,15700124,15700149,15700186,15700190,15700201,15700214&usg=ALkJrhhzOhaLrScUn08Avq9yMriYEEAkLw

I do not understand everything but I use JPA version 2.1, I do not know the specification of the latter, but there is a way around the problem.

Add that the English language is not my mother tongue and I have trouble translating!

could anyone help me?
 
Sheriff
Posts: 21466
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your primary key class needs to have an equals and hashCode method.

When I create primary key classes I never put the associations in it, only fields matching their primary keys. The association then goes in the entity class with @MapsId. For instance:

You will need to make sure to keep the RouteId instance and the many-to-one fields in sync. Best way to do this is to make the non-argument constructor protected, and add a public constructor that takes these fields and uses them to create the RouteId instance for you. You don't want any setters anyway, because you should never update your primary key.
 
Philippe Ponceblanc
Ranch Hand
Posts: 183
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I admit that I have trouble following, now I have multiple mistakes which I know what to do to solve them.

Here's my code, I hope I'm not wrong:

>> Class Route



>> Class RouteId



if you have URL link to give me to understand I want, because this way of coding comes out of the course !

Regards
Philippe
 
Rancher
Posts: 3767
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What errors are you getting?

(Note that the RouteId is supposed to have getters and setters)
 
Philippe Ponceblanc
Ranch Hand
Posts: 183
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:What errors are you getting?
(Note that the RouteId is supposed to have getters and setters)





Errors are annotated in green writing with error !


Class RouteId :



Class Route :

 
Dave Tolls
Rancher
Posts: 3767
40
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the first one, I think this was mentioned earlier.
The class cannot be final as JPA/Hibernate needs to be able to extend the class as part of how it handles persistence.

The next says exactly what the issue is (again, I think this was brought up earlier).  You need valid equals and hashcode methods.

The following three imply that the primary keys for those classes are not Long, which is what you are using in the RouteId class.
The fields have to match.

 
Philippe Ponceblanc
Ranch Hand
Posts: 183
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:For the first one, I think this was mentioned earlier.
The class cannot be final as JPA/Hibernate needs to be able to extend the class as part of how it handles persistence.

The next says exactly what the issue is (again, I think this was brought up earlier).  You need valid equals and hashcode methods.

The following three imply that the primary keys for those classes are not Long, which is what you are using in the RouteId class.
The fields have to match.




1/ RouteId Class == OK

2/ i put valid equals and hashcode methods === OK

3/ yet the errors that i don't anderstand in Route Class == ???
   I do not repeat the mistakes but I must also assimilate, otherwise it is useless, if I do not understand what I code.
   Nothing is going to be an example with my code so that I understand a good time, that will serve me as reference code!






 
Dave Tolls
Rancher
Posts: 3767
40
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that first one is saying you need an equals and hashcode in the RouteId class.
It needs that to ensure that it can tell when one instance of Route is representing the same row as another instance.

For the others errors, what are the primary keys in Airline and Airport?
That is, what are the @Id field(s)?

They fields for the Ids in RouteId need to be of the same type.
 
Rob Spoor
Sheriff
Posts: 21466
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. I assumed they are Longs because that's what I usually use.

I am a bit surprised that the RouteId class can't be final, as all our primary key classes are final. This may be caused by a different Hibernate version though. Easy solution: just remove the final keyword.

It's obvious that your hashCode and equals methods are generated, because only an IDE (Eclipse, right?) can produce such horrors. FYI, here's how I would rewrite that (Java 7+ only):
 
Stephan van Hulst
Bartender
Posts: 9498
184
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not:
 
Philippe Ponceblanc
Ranch Hand
Posts: 183
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I have understood a little, except that I have three errors left, the purpose of which I do not understand.


>> Class Route




>> Class Airline



>> Class Airport



>> Class RouteId

 
Dave Tolls
Rancher
Posts: 3767
40
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Airline and Airport use ints for their ids.
Your RouteId class uses Long.

That's why JPA is complaining about:
The type of the ID mapped by the relationship 'source' does not agree with the primary key class of the target entity.
 
Philippe Ponceblanc
Ranch Hand
Posts: 183
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Airline and Airport use ints for their ids.
Your RouteId class uses Long.

That's why JPA is complaining about:
The type of the ID mapped by the relationship 'source' does not agree with the primary key class of the target entity.



thanks you!

Post/thread Solved
 
Rob Spoor
Sheriff
Posts: 21466
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Why not:


Simple - it creates one extra array (because of varargs).
 
Stephan van Hulst
Bartender
Posts: 9498
184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think writing up your last response already took more time than the cumulative total of difference in runtime of all executions of that application.

I doubt the temporary addition of 50-100 bytes on the stack is that much of a big deal either.
 
Bartender
Posts: 19996
95
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should be using @IdClass. That's what I do, anyway.
 
Rob Spoor
Sheriff
Posts: 21466
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I doubt the temporary addition of 50-100 bytes on the stack is that much of a big deal either.


If it's on the stack then there is no problem. I thought that the array was created on the heap. That adds garbage collection overhead (and we've had IT OPS breathing down our necks that there was too much GC time). Do you have some article (or the JLS) that shows that vararg arrays are indeed created on the stack?
 
Stephan van Hulst
Bartender
Posts: 9498
184
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, unless Objects.hash() does weird things with the array, like passing it around to other method calls, escape analysis can determine that the object can be allocated on the stack.

Check out this question on StackOverflow: https://stackoverflow.com/questions/43002528/when-can-hotspot-allocate-objects-on-the-stack
 
Rob Spoor
Sheriff
Posts: 21466
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes quite a bit of sense.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!