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 8655443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:01:41+00:00 2026-06-12T15:01:41+00:00

I have a one-to-one relation like this The Parent @JsonAutoDetect @Entity @Table(name = Parent)

  • 0

I have a one-to-one relation like this

The Parent

@JsonAutoDetect
@Entity
@Table(name = "Parent")
public class Parent{

    private Integer id;
    private Child child;

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column (name="idparent")
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parent")
    @JoinColumn(name="idchild", nullable=true)
    public Child getChild() {
        return child;
    }
    public void setChild(Child child) {
        child.setParent(this);
        this.child = child;
    }
}

and the child

@JsonAutoDetect
@Entity
@Table(name="Child")
public class Child{

    private Integer id;
    private Parent parent;

    @Id
    @GeneratedValue(generator = "foreign")
    @GenericGenerator(name = "foreign", strategy = "foreign", parameters = { @Parameter(name = "property", value = "parent") })
    @Column (name = "idchild")
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }

    @OneToOne(fetch = FetchType.LAZY, optional = true)
    @Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE })
    @PrimaryKeyJoinColumn
    @JsonIgnore
    public Parent getParent() {
        return parent;
    }
    public void setParent(Parent parent) {
        this.parent= parent;
    }

}

I want to create criteria that return the parent that have child isNotNull, i tryed somme think like

Criteria criteria = session.createCriteria(Parent.class);
criteria.add(Restrictions.isNotNull("child"));

But not work, can you help me by giving somme example please ? Thanks

  • 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-12T15:01:42+00:00Added an answer on June 12, 2026 at 3:01 pm

    First of all, the mapping is wrong. In the parent class, you’re saying that the association is mapped by child.parent, and immediately after you’re saying that it’s mapped using a join column named id_child. Make up your mind. Either it’s mapped by the child.parent property, and you should remove the @JoinColumn. Or it’s mapped by the JoinColumn and you should remove the @PrimaryKeyJoinColumn in the child, and use mappedBy="child" in the Child entity.

    Now, to make your query work, whatever the mapping is, you should simply make an inner join:

    Criteria criteria = session.createCriteria(Parent.class);
    criteria.createAlias("child"); // inner join
    

    Since the join is an inner join, it will only select parents that have a non-null child.

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

Sidebar

Related Questions

I have one-to-many parent-child entity Book <->> Borrow Book name borrows (relation) Borrow borrowDate
I have table in that there are relation between Parent and Child, in this
I have a one to many relation that looks like this: | Parent |
I have a one-to-many relation between Parent and Child table. In the parent object
I have multiple level parent-child tables(GrandParent, Parent and Child, all many to one relation,
i have a relation (one to many/ many to one) between parent and child
I have two Doctrine entities that have a one-to-many relationship, like this: License class
I have a parent/child relation table, many to many parentid int not null childid
Lets say I have two tables - child and parent with many-to-one relation. What
i have an entity Entity1 that have one to many relation with Entity2 as

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.