• 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Is there such a thing as One to One relationship in this case?

 
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Happy new year 2021 to coderanch experts.

I am stuck in making my OrderController work and wish to clarify if there is any one-to-one relationship mapping needed at all.

In my Order Entity, there is a petId that appear there.



So the initial code that works just doesn't work now with the change of JPA aka hibernate coming into the picture.

I am wondering it has to do with any one-to-one mapping needed in this case?

But, I searched the internet the examples with One-to-One mappings only happened if I have Pet pet inside Order entity.

Appreciate some advice in this case.

Thank you.
 
Sheriff
Posts: 22053
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have an id field only, you do not have a JPA relationship. There may be a relationship on a database level, but JPA needs entities, not ids.
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:If you have an id field only, you do not have a JPA relationship. There may be a relationship on a database level, but JPA needs entities, not ids.



Hi Rob,

Happy new year to you and your family!

Could you clarify in this case do I have to manually set up the relationship like a foreign key/Primary key stuff manually in the MySQL on top of the automatic tables generated by hibernate?

I am really confused because I was also told by others that what I am doing is all for development stage.  In production, I may need to do a real MySQL with all the traditional SQL so then how do the JPA comes in?

Cos now the old code that worked just won't work anymore and I kept getting the petId not found but it was obvious the petId was created and get inserted into the db...

Hope you could advise me what's causing all these problems...

Tks.
 
Bartender
Posts: 2006
13
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tangara,
First of all, you need @Id for the orderId.
Secondly, you may need something like this:

@OneToOne
private Pet pet;

In this way, the JPA will set a feign key for the Pet.

Reference:
https://www.baeldung.com/jpa-entities
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have changed it to use OneToOne as follows but then I realised that if I were to do it this way, then I will not be able to use the getPetId method in Order.



If I were to put back the int Pet_Id then I will get lots of errors that I have a error :



Could you let me know what is the right way to do it?
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Himai Minh wrote:Tangara,
First of all, you need @Id for the orderId.
Secondly, you may need something like this:

@OneToOne
private Pet pet;

In this way, the JPA will set a feign key for the Pet.

Reference:
https://www.baeldung.com/jpa-entities



Hi Himai,

Please see my reply and let me know the correct way for my usecase can ? Tks.
 
Rob Spoor
Sheriff
Posts: 22053
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not just use order.getPet().getId()?
 
Himai Minh
Bartender
Posts: 2006
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Tangara,
I think you can omit @PrimaryKeyJoinColumn.
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Himai Minh wrote:Hi, Tangara,
I think you can omit @PrimaryKeyJoinColumn.



If I took that off, I will get error :


Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: model.Order column: pet_id (should be mapped with insert="false" update="false"



But, how can I have insert and update = false cos I definitely I need Order to be updated with the pet_id and insert with pet_id.

Furthermore, I would like to seek your advice on Order checking on PetStatus which is a Enum class.

Do I need additional mapping in Order so that it is aware the PetStatus of the PetId ?

How do I do the mapping in this case?

Tks.
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:Why not just use order.getPet().getId()?



Dear Rob,

Happy new year to you and your family.

You saved my life !!!

It works !!!

Now, I need to check on the status the next step....

Thank you.  You are a huge help !

Why I can't think of something so straightforward and went around using the various end point method instead ???

Thank you so much !

After further testing, I find that I can't make it work ...

So, this is to further confirm with you if it is because I can't use order.getpetId at all because I need the Order to recognise that the petId and its associate PetStatus with that petId

Thus, could you confirm if


is equivalent to



Tks.

Thanks.
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Himai Minh wrote:Hi, Tangara,
I think you can omit @PrimaryKeyJoinColumn.



Hi Himai,

Can you advise me if there is any mapping needed at the Enum PetStatus there in order for it to work via Hibernate or JPA ?
Actually, I am quite confused because is SpringBoot relying on underlying Hibernate to do the mapping etc ?

Hope you can advise me because till now I still can't get it work.  Tks.
 
Himai Minh
Bartender
Posts: 2006
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Tangara,
You may find this tutorial helpful https://www.baeldung.com/jpa-persisting-enums-in-jpa
You may need to annotate the field by @Enumerated(EnumType.String).
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Himai Minh wrote:Hi, Tangara,
You may find this tutorial helpful https://www.baeldung.com/jpa-persisting-enums-in-jpa
You may need to annotate the field by @Enumerated(EnumType.String).



Hi Himai,

I already put the necessary annotation like this EnumType but my question is how will the Order knows the PetStatus in Pet ?

And the Mapping so far for @OneToOne only let the Order knows about PetId without knowing about the PetStatus together with the PetId and it's status.

This is the part that I am teribbly confused.

Can you advise me on the issue I am facing?
 
Himai Minh
Bartender
Posts: 2006
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fields in your Pet class will be mapped to the database table as Pet is annotated with @Entity.
Do you get a chance to see the table generated ?
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Himai Minh wrote:The fields in your Pet class will be mapped to the database table as Pet is annotated with @Entity.
Do you get a chance to see the table generated ?



Hi Himai,

Actually, I am rather confused as to which entity I should do the Mapping.  Should I do it at the Pet there for @OneToOne for a Unidirectional relationship or at the Order there.

How can I know ?

Cos I read before the owning side is very different from the normal thinking....as in I feel that Order is the one that I have to put @OneToOne but it seems like Pet is the one that I should do it.

Can you let me know how should we determine the annotation to be put on which entity?

Another question is Once I have the @OneToOne annotation, I will have to remove the pet_Id in Order is that right cos the OneToOne will do a auto mapping to the pet_Id right?

In this case, if I remove it, I will have problem in the Service layer where I need to use Order to get the petId.

How should I handle it then ?


Thanks.
 
Himai Minh
Bartender
Posts: 2006
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OneToOne can be uni-directional or bidirectional.
If the @OneToOne is uni-directional, it is in one class.  That class is the owner.
If it is bidirectional, it is in two classes. One class is the owner. The other one is being owned.
The owner has a feign key refering to the being owned entity.
I highly recommend this book :
https://www.amazon.com/Pro-JPA-Mastering-Persistence-Technology/dp/1430219564/ref=sr_1_3?dchild=1&keywords=JPA+Pro&qid=1610031880&sr=8-3
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Himai Minh wrote:@OneToOne can be uni-directional or bidirectional.
If the @OneToOne is uni-directional, it is in one class.  That class is the owner.
If it is bidirectional, it is in two classes. One class is the owner. The other one is being owned.
The owner has a feign key refering to the being owned entity.
I highly recommend this book :
https://www.amazon.com/Pro-JPA-Mastering-Persistence-Technology/dp/1430219564/ref=sr_1_3?dchild=1&keywords=JPA+Pro&qid=1610031880&sr=8-3



Hi Himai,

After studying here and there, I think mine I need bi-directional.

Now, I have a question that I need your advice :

I got someone to tell me a way to handle my use case but I am abit stunned by the way it is done.
So, I have now changed my interfaces to all inherited JpaRepositoy.

Now, I have a use case that I need to use JPA or hibernate ORM to take over :

and since I don't have a end point that gives me which Pet has the status SOLD, I was told I can just create a method like that and then the ORM can do the job.

Is this real ?



But, I can't get the result I want.

When I did a debug, it shows that order.getPet() is null.

Does that mean that there is something amissed in my mappings ?



 
Himai Minh
Bartender
Posts: 2006
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your PetStatus saved to the Pet table?
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Himai Minh wrote:Is your PetStatus saved to the Pet table?



Yes.  

But, the thing now is that even though I had the logic working, the db side do not have data entered into the Order; only the Pet.

How can we debug to know what is causing the data not to be entered into the Order table, even though Postman is giving me 200ok for Pet that is not sold, therefore an Order can be created.

Hope you could advise me.

Once again, thank you so much.




 
Himai Minh
Bartender
Posts: 2006
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, do you have any repository that can be used to save Order instance?
reply
    Bookmark Topic Watch Topic
  • New Topic