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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:53:58+00:00 2026-05-12T21:53:58+00:00

I ‘m trying to build a user and a contact management project. i have

  • 0

I ‘m trying to build a user and a contact management project.
i have a lot of classes so will limit it to what are in concern here. i have a userAccount, userProfile, and group
here is the UserAccount Mapping

@Id @GeneratedValue
@Column(name="USER_ACCOUNT_ID")
private Long ID; 
 ......
 @Column(name="EMAIL", length=100, unique=true)
private String email;

@OneToOne(targetEntity=UserProfileImpl.class,cascade={CascadeType.ALL})
 @org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
@JoinColumn(name="USER_PROFILE_ID")
private UserProfile profile;

 @OneToMany(targetEntity=GroupImpl.class, cascade={CascadeType.ALL})
 //    @JoinColumn(name="USER_ACCOUNT_ID")
@JoinTable(name = "USER_ACCOUNT_CONTACT_GROUP", joinColumns = @JoinColumn(name = "USER_ACCOUNT_ID"), inverseJoinColumns = @JoinColumn(name = "GROUP_ID"))
@org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Set<Group> groups = new HashSet<Group>();
........

here is the userProfile

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="USER_PROFILE_ID")
private Long ID;
.....
 @OneToOne(mappedBy="profile", targetEntity=UserAccountImpl.class)
private UserAccount userAccount;
.....

here is the group

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="GROUP_ID")
private Long ID;    
.......
@Column(name="NAME", length=100, unique=true)
private String Name;

@ManyToOne(targetEntity=UserAccountImpl.class)
@JoinColumn(name="USER_ACCOUNT_ID",nullable=false)
private UserAccount userAccount;

so basically this is it.Everything went fine before a did a bidirectional association adding userAccount object to group.so on testing. i create userProfile, userAccount ,and group(using spring @autowired) .
On the setUP (@Before) method i set few prop to userProfile and add it to useAccount along with its prop and persist it and then set the useAccount to the group.
on the tearDown(@After) i delete the userACcount .
on my testSave (saves the group) works fine (using the same session for all the operations) but there is no delete sql query on the console for the group but it deletes the userAccount Though.
By deleting the userAccount in the teardown method i was hoping group should be deleted to.But that that’s not the case.
worse because of the unique constraint on name of the group the other tests are failling.I search on the net but everything i try doesn’t seem to work.Who can save me 🙂 ? i mean how am i supposed to do it? thanks for reading

this is an update on what i’ve tried based on response i’ve got.
i’ve added this to group.setUserAccount(null) to my removeGroup which is now this

public void removeGroup(Group group) {
    try{
       if(this.groups.contains(group)){
        group.setUserAccount(null);
        this.groups.remove(group);
       }
    } catch(Exception ex){
        ex.printStackTrace();
    }
}

so for the test i used which has the same behavior ie deletes the userAccount but not the group

ua1.removeGroup(g1);
uaDao.delete(ua1);

i guess it’s not entering the if block because i should have the same behavior as this one:

 g1.setUserAccount(null);
 uaDao.delete(ua1);

this other one throws an error

PropertyValueException: not-null property references a null or transient value

so i think i’ll delete the group using :

gDao.delete(g1);
uaDao.delete(ua1);

hoping that i might find a way to go about it.Thanks to people who have helped me out especially Pascal if you had another idea or found something wrong about my code i’ll be more than glad to correct it.thanks for reading

  • 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-12T21:53:58+00:00Added an answer on May 12, 2026 at 9:53 pm

    First off if the UserAccount -> Group relationship is bi-directional then you would need to map the inverse collection side as

    @OneToMany(targetEntity=GroupImpl.class, cascade{CascadeType.ALL},mappedBy="userAccount")
    

    So now Hibernate knows that the UserAccount is the owner of the collection. Now for deleting as @Pascal says you need to unbind both sides. Since Hibernate ignores the collection, the owner is responsible for binding the relationship so basically a helper method like this creates the association in UserAccount

    public void addGroup(Group group){
         group.setUserAccount(this);
         groups.add(group);
    }
    

    now for deleting another helper method

    public void removeGroup(Group group){
        group.setUserAccount(null);
        groups.remove(group);
    }
    

    now for removal

    UserAccount ua = //load user account object
    for(Group g : ua.getGroups())
          ua.removeGroup(g);
    
    session.delete(ua);
    

    Your code should work now. You don’t have to do it my way but I hope you get the drift.DELETE_ORPHAN should take care of your cascade deletion of groups

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

Sidebar

Related Questions

I am trying to create a RegEx expression that will successfully parse the following
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
Hi if i run my project its indicate this logcat error...how to solve this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have a reasonable size flat file database of text documents mostly saved in

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.