Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when changing to OneToOne relationship #29

Closed

Conversation

cca-developer
Copy link

This test works fine with a ManyToOne/OneToMany relationship between PostTag and Tag. However, if that is changed to a OneToOne relationship, the test no longer passes.

This test works fine with a ManyToOne/OneToMany relationship between PostTag and Tag.  However, if that is changed to a OneToOne relationship, the test no longer passes.
@vladmihalcea
Copy link
Owner

Why would you change it to OneToOne? This is a many-to-many association. Do you want to map a one-to-one association with an intermediary table?

@cca-developer
Copy link
Author

I have a situation where I have WorkOrders and Inspections. A WorkOrder can have many Inspections attached to it, but an Inspection can only be attached to one WorkOrder. Also, a WorkOrder may not have any inspections. If a WorkOrder has one or more inspections, there needs to be some extra information attached to each inspection, which I why I want the extra join table. But it is not a straight one-to-one association because a WorkOrder could have many inspections. If I didn't need the extra information, this would obviously be really easy. I could make it look like a many-to-many association, but that is somewhat confusing because an Inspection entity would never have a list of associated work orders. Does that use case make sense?

@vladmihalcea
Copy link
Owner

A One-to-one child association cannot have a OneToMany on the other side. It can have a One-to-one.

@cca-developer
Copy link
Author

Sorry, I just realized that I had only changed the relationship on your code and hadn't changed anything else. Does it make more sense now what I am trying to do? Sorry about that!

@cca-developer
Copy link
Author

The test now successfully passes. I'm not sure why it is not working on my code. Does this seem like a legitimate way to set up a relationship now?

@cca-developer
Copy link
Author

I did finally get this to work in my program. I had been thinking that the MapsId annotations were referring to a database field, not a property in the PostTagId class. I'm still curious on your thoughts about this relationship. Thanks!

@vladmihalcea
Copy link
Owner

I'm glad you found the solution. As for the mapping, I never had to use it. Probably you can simplify it to a OneToMany association and merge the two one-to-one sides.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants