Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7729351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:56:46+00:00 2026-06-01T05:56:46+00:00

I’ve got Parent – Child relationship OneToOne, but two of them. Annotations are not

  • 0

I’ve got Parent – Child relationship OneToOne, but two of them. Annotations are not good but it produces good DB schema, yet code is not working. If I try to save Parent instance, Hibernate at first tries to save child1 and child2 – but it breaks FK defined in Child -> because owner doesn’t exist yet in DB…So I need to save Parent and then child1, and child2.

If I could do that it doesn’t help, because when I try to load Parent, Hibernate will not know which record in Child table belongs to child1 or child2…So in this case I would need to specify one condition in join for child1 something like “where type = 1” and for child2 “where type = 2″…

Just to clarify: in Child table there will be zero or one child for one Parent with ChildType.A (always child1) and zero or one child with ChildType.B (always child2).

I need to save xml which looks like this:

<parent id="" oid="">
   <child1 (and other attributes)>
   <child2 (and other attributes)>
<parent>

Both child1 and child2 elements are the same type therefore are type of Child in java classes. Only difference is element name (in java I differentiate them with ChildType). Only identification for children are id and oid attributes from parent. They points to another Parent hence target in Child.

I need to change annotations somehow to get this working…Do you guys have some ideas, because I’m really stuck???

Parent.java

public class Parent {

    private String oid
    private Long id;

    private Child child1;
    private Child child2;

    @Id
    @GeneratedValue(generator = "IdGenerator")
    @GenericGenerator(name = "IdGenerator", strategy = "com.example.IdGenerator")
    @Column(name = "id")
    public Long getId() {
        return id;
    }

    @Id
    @GeneratedValue(generator = "OidGenerator")
    @GenericGenerator(name = "OidGenerator", strategy = "com.example.OidGenerator")
    @Column(name = "oid", nullable = false, updatable = false, length = 36)
    public String getOid() {
        return oid;
    }

    @OneToOne(optional = true, fetch = FetchType.EAGER)
    @Cascade({org.hibernate.annotations.CascadeType.ALL})
    public Child getChild1() {
        return child1;
    }

    @OneToOne(optional = true, fetch = FetchType.EAGER)
    @Cascade({org.hibernate.annotations.CascadeType.ALL})
    public Child getChild2() {
        return child2;
    }
}

Child.java

public class Child {

    private Parent owner;
    private String ownerOid;
    private Long ownerId;
    private ChildType type;

    private Parent target;

    @MapsId("owner")
    @ManyToOne(fetch = FetchType.LAZY)
    @PrimaryKeyJoinColumns({
            @PrimaryKeyJoinColumn(name = "owner_oid", referencedColumnName = "oid"),
            @PrimaryKeyJoinColumn(name = "owner_id", referencedColumnName = "id")
    })
    public Parent getOwner() {
        return owner;
    }

    @MapsId("target")
    @ManyToOne(fetch = FetchType.LAZY)
    @PrimaryKeyJoinColumns({
            @PrimaryKeyJoinColumn(name = "target_oid", referencedColumnName = "oid"),
            @PrimaryKeyJoinColumn(name = "target_id", referencedColumnName = "id")
    })
    public Parent getTarget() {
        return target;
    }

    @Id
    @Column(name = "owner_id")
    public Long getOwnerId() {
        if (ownerId == null && owner != null) {
            ownerId = owner.getId();
        }
        return ownerId;
    }

    @Id
    @Column(name = "owner_oid", length = 36)
    public String getOwnerOid() {
        if (ownerOid == null && owner != null) {
            ownerOid = owner.getOid();
        }
        return ownerOid;
    }

    @Id
    @Column(name = "target_id")
    public Long getTargetId() {
        if (targetId == null && target != null) {
            targetId = target.getId();
        }
        return targetId;
    }

    @Id
    @Column(name = "target_oid", length = 36)
    public String getTargetOid() {
        if (targetOid == null && target != null) {
            targetOid = target.getOid();
        }
        if (targetOid == null) {
            targetOid = "";
        }
        return targetOid;
    }

    @Id
    @Enumerated(EnumType.ORDINAL)
    public ChildType getType() {
        if (type == null) {
            return ChildType.A;
        }
        return type;
    }
}

ChildType.java

public enum ChildType {
    A, B;
}

I also tried to use mappedBy approach mappedBy approach but there are still problems with loading – I can’t tell hibernate which child record belogs to which child class member variable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:56:47+00:00Added an answer on June 1, 2026 at 5:56 am

    there are too many things I don’t quite get in your solution to give a good answer but just some thoughts:

    Consider using inheritance instead of ChildType enum. So you would have ChildA and ChildB extending Child.

    That way you Parent can have:

    private ChildA child1;
    private ChildB child2;
    

    Instead of having a composite primary key, I would consider using a unique auto generated key and then add a unique constraint on on the other id and oid fields. It should make the child parent relationships easier and you can have different parent implementation for ChildA and ChildB:

    In ChildA:
    @OneToOne(mappedBy="child1")
    public Parent getParent() {
        return parent;
    }
    

    And

    In ChildB:
    @OneToOne(mappedBy="child2")
    public Parent getParent() {
        return parent;
    }
    

    And in Child just:

    public abstract Parent getParent();
    

    Now the whole Parent/Owner/Target thing I still didn’t quite grasp.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I'm trying to select an H1 element which is the second-child in its group
i got an object with contents of html markup in it, for example: string
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.