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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:37:45+00:00 2026-06-15T15:37:45+00:00

I have @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) public Linf getLinf() { return linf;

  • 0

I have

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
public Linf getLinf() {
    return linf;
}

and

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, targetEntity = MessageEntry.class)
public Set<MessageEntry> getMessageEntries() {
    return messageEntries;
}

I need to remove single messageEntry from database. If I say sess.delete(messageEntry), then I get index exception since its in Linf.messageEntries collection. Stateless session can’t load collection, so I have
to load elements of Linf.messageEntries mannually and then remove one of them:

            List linfs = sess.createQuery(
                    "SELECT l FROM Linf l " +
                            "JOIN l.messageEntries e WHERE e=:e")
                    .setParameter("e", messageEntry).list();

            if (linfs.size()>1) throw new RuntimeException();

            Linf linf = (Linf) linfs.get(0);

            List<MessageEntry> curEntries =
                    sess.createQuery(
                            "SELECT e FROM Linf l " +
                            "JOIN l.messageEntries e WHERE l=:l")
                            .setParameter("l", linf).list();

            for (int i = 0; i < curEntries.size(); i++) {
                if (curEntries.get(i).getId().equals(messageEntry.getId())) {
                    curEntries.remove(i);
                    break;
                }
            }

            Set<MessageEntry> cur = new HashSet<MessageEntry>();
            cur.addAll(curEntries);
            linf.setMessageEntries(cur);

            messageEntry.setLinf(null);
            sess.update(messageEntry);
            sess.update(linf);
            sess.delete(messageEntry);

I get ConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (db.linf_messageentry, CONSTRAINT FK5039A8B5E770809A FOREIGN KEY (messageEntries_id) REFERENCES messageentry (id)). How can I perform this task? Thank you.

  • 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-15T15:37:46+00:00Added an answer on June 15, 2026 at 3:37 pm

    Your mapping is wrong. Instead of having a bidirectional one-to-many association, using a foreign key in MessageEntry, you have a OneToMany association using a join table (linf_messageentry), and another, distinct, ManyToOne association using a foreign key.

    The one side must be marked as the inverse of the many side, using the mappedBy attribute:

    // This is the owner side of the association, because it doesn't have 
    // the mappedBy attribute.
    // it uses a join column by default
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    public Linf getLinf() {
        return linf;
    }
    

    and

    // This is the inverse side, because it has the mappedBy attribute
    // since it's mappedBy linf, hibernate uses the same mapping as the one
    // described on the linf property: a join column
    @OneToMany(mappedBy = "linf", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    public Set<MessageEntry> getMessageEntries() {
        return messageEntries;
    }
    

    Also, note that:

    • the targetEntity attribute is completly redundant: Hibernate knows the target entity from the type of the collection: Set<MessageEntry>
    • cascade = CascadeType.ALL on a ManyToOne doesn’t make much sense. If you delete a MessageEntry, you don’t want its Linf to be deleted.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have relationship: // In A.java class @OneToMany(mappedBy=a, fetch=FetchType.LAZY) @Cascade(CascadeType.SAVE_UPDATE) private List<B> bList; //
I have bi-directional relationship like this... Person.java public class Person{ @JsonIgnore @OneToMany(targetEntity=PersonOrganization.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER,
I have an Entity like this: public class Configuration { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name
We have an entities like this: public User { @OneToMany(fetch = FetchType.EAGER, cascade =
I have Person entity which has composition with Location Entity @ManyToOne(fetch = FetchType.EAGER, cascade
I have a class say: ClassA which has a collection of ClassB @OneToMany(cascade =
I have two entities like the following: @Entity public class Trip { @OneToMany(mappedBy =
I have Persistent class, referring to many collections with LAZY fetch type, like @Entity
In the entity Invoice, I have a set of DetailInvoice with cascade is ALL
I have the following two entities: @Entity class Relation{ @ManyToOne private User user; //some

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.