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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:04:17+00:00 2026-05-20T16:04:17+00:00

I have many-to-many for tag <-> software , when i delete tag i want

  • 0

I have many-to-many for tag <-> software, when i delete tag i want delete all softwares which are related to that tag BUT not which are related to another tag. The business logic here is the following: Sofware can not exist without a tag. There are code with two classes and one test below.

Now it deletes all softwares even if they are related to other tags.

How to handle it?

@Entity
public class Tag extends Model {

    @Column(nullable = false, unique = true)
    public String title;

    public Tag(String title) {
        this.title = title;
    }

    @ManyToMany(cascade = CascadeType.ALL, mappedBy = "tags")
    public List<Software> softwares = new LinkedList<Software>();

}


@Entity
public class Software extends Model {

    public String title;
    public String description;

    @ManyToOne(optional = false)
    public Author author;

    @ManyToMany
    public List<Tag> tags = new LinkedList<Tag>();

    public Software(String title, String description, Author author) {
       this.title = title;
       this.description = description;
       this.author = author;
    }


    public Software(String title, String description, Author author, Tag ... tags) {
        this(title, description, author);


        if (!Arrays.asList(tags).contains(null)) {

            // it needed if we wand to delete Tags with cascade - when delete Tag, then delete all Softwares related to it
            for (Tag tag : tags) {
               this.tags.add(tag);
            }

            for (Tag tag : tags) {
                tag.softwares.add(this);
            }
        }
    }

 }

there is a test:

@Test
public void testDelete() throws InterruptedException {

    Tag tag1 = new Tag("tag1").save();
    Tag tag2 = new Tag("tag2").save();

    Author author1 = new Author("name", "email").save();


    new Software("title1", "description1", author1, tag1).save();

    new Software("title3", "description3", author1, tag1, tag2).save();


    tag1.delete();

    // try to find the software
    assertEquals(1, Software.findAll().size());  // IT FAILS - IT DELETES ALL
}
  • 1 1 Answer
  • 2 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-20T16:04:18+00:00Added an answer on May 20, 2026 at 4:04 pm

    Triggers, or logic in your service layer is the best bet to achieve it. If you really want it via model, normalize it, here you have a choice to do in the behavior on delete.

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

Sidebar

Related Questions

I have: image 1:Many imageToTag Many:1 tag I want to issue a query that
I have an Entry model which has many Tag s. Tag s are added
I have created many tables using <table> tag but don't know how to get
I have 2 Entities, related by a many-to-many relationship. Thing<<->>Tag There is one NSArrayController
I have many flags that I want to store them in an integer, so
I'm using wordpress and i have many plugins which using javascript file. I want
JavaScript goes here.. and i have many sections of div tag.. i want to
I have looked throughout the greatest-n-per-group tag and found great information but nothing that
I'm pretty sure that many people have thought of this, but for some reason
I have following html code. In my page there are many tag which have

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.