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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:43:26+00:00 2026-05-20T08:43:26+00:00

i have the following classes: //Product class public class Product { @Id @GeneratedValue private

  • 0

i have the following classes:

//Product class
public class Product {
@Id
@GeneratedValue
private Long id;
private String name;

//Package Class
public class Package {
@Id
@GeneratedValue
private Long id;

@OneToOne(cascade=CascadeType.ALL)
private Product product;

private int quantity;
private char mode;
private String unity;
private String description;

@OneToOne(cascade=CascadeType.ALL)
private Usuario user;

public class User {
    @Id
    private String email;

I can insert a product, an user and a package just doing:

    //.. (iniatilize product and user before and set in package object) 
    session.save(package);
    session.getTransaction().commit();

So why i can’t delete an user ?

    User user = new User();
    user.setEmail("valter@gmail.com");
    session.delete(user);
    session.getTransaction().commit();

Gives me this error :

Cannot delete or update a parent row:
a foreign key constraint fails

What’s wrong with my classes ?

Complete Entity User

@Entity
public class Usuario {
    @Id
    private String email;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

@Entity
public class Package{
    @Id
    @GeneratedValue
    private Long id;

    @OneToOne(cascade=CascadeType.ALL)
    private Product product;

    private int quantity;
    private char mode;
    private String unity;
    private String description;

    @OneToOne(cascade=CascadeType.ALL)
    private User user;


@Entity
public class Product {
    @Id
    @GeneratedValue
    private Long id;
    private String name;

That’s all entities envolved in the system.

@Bozho Based in what you say , it should be this way right?

@Entity
public class User{
    @Id
    private String email;

    @OneToOne(cascade=CascadeType.ALL)
    private List<Package> packages;

I’m trying to execute this method:

    private static void deleteUser() {
    Session session = new HibernateUtil().getSession();
    session.beginTransaction();

    try{

        User usuario = (Usuario) session.load(Usuario.class, "valter@gmail.com");
        session.delete(usuario);
        session.getTransaction().commit();


    }catch(HibernateException he){
        session.getTransaction().rollback();
    }finally{
        session.close();
    }
}
  • 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-05-20T08:43:27+00:00Added an answer on May 20, 2026 at 8:43 am

    It appears you are creating a detached entity and trying to delete it, rather than fetching a complete entity from the DB. In this case, even if you have cascades defined, they won’t work because and relationship of the User entity is not populated.

    If the email is the primary key, then you should do the following:

    User user = session.load(User.class, "foo@bar.com");
    session.delete(user)
    

    Even with that, if any other entity references a User, it will logically fail – the just look at the database constraints. You have multiple ways to fix this:

    • map List<Package> in your user, thus making the relationship bi-directional. Thus you’ll be able to set the cascade on the User side as well
    • before deleting a user find (by a query) all packages that belong to him, and delete them.
    • If the package does not have any other cascades, use an HQL query: DELETE FROM Package WHERE user=:user".
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following classes: public class CartItem { public long Id { get;
I have the following simple textbook classes defined: public class Product { public int
Suppose, I have following classes: public class DisposableObj : IDisposable { public ChildObj CreateObj();
I have the following classes defined to do validation: public class DefValidator : IValidate<IDef>
I have the following classes: class Vigil < ActiveRecord::Base after_update :do_something_cool private def do_something_cool
I have the following classes in my Model: public abstract class Entity : IEntity
I have the following classes [XmlRoot] public class AList { public List<B> ListOfBs {get;
The scenario Suppose I have the following two model classes: public class ProductColor {
If i have the following classes public class Order { public virtual ISet<OrderItem> OrderItems
I have these classes: public class Product { [Key] public virtual int ProductId {

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.