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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:13:52+00:00 2026-05-20T17:13:52+00:00

I have two entities within a relationship tag <-m:n-> software and I want to

  • 0

I have two entities within a relationship

tag <-m:n-> software

and I want to delete all softwares which are not links to tags anymore after deleting particular tag. I write the HQL query for that..

i use playframework

my overridden Tag.delete()

@Override
public Tag delete() {

    Tag t = null;

    // t = super.delete();   // commented for now

    // it should delete ONLY that softwares which are not linked with tags (tags is empty)
    Query q = Tag.em().createQuery("delete from Software s where s.tags is empty ");
    q.executeUpdate();

    return t;
}

my 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();

    Software s1 = new Software("soft1", "description1", author1, tag1).save(); // this should be deleted when tag1 is deleting

    Software s2 = new Software("soft2", "description2", author1, tag1, tag2).save(); // this should be deleted, because it links to tag2

    // checks, just in case: 
    Software ss = Software.findById(s1.id);
    assertTrue(ss.isPersistent());
    assertTrue(!ss.tags.isEmpty());
    assertEquals(1, ss.tags.size());

    tag1.delete();

    // try to find the software
    assertEquals(1, Software.findAll().size()); // here it faults, it deletes all!!!
}    

now i have the problem that it deletes ALL softwares, even if they have links to tag.

but i get SQL which is formed from HQL it is like:


delete from Software where not (exists (select tag.id from Tag_Software ts, Tag tag where Software.id=ts.softwares_id and ts.tags_id=tag.id))

and it is good SQL (i checked it), but why all this does not work as HQL in my context…?

my test says:

Failure, expected:<1> but was:<0>

the code for two classes are:

public class Tag extends Model {

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

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

    @ManyToMany
    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(mappedBy = "softwares")
    public List<Tag> tags = new LinkedList<Tag>();

…

  • 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-20T17:13:53+00:00Added an answer on May 20, 2026 at 5:13 pm

    There are multiples ways:

    `softwares.size = 0

    size(softwares) = 0
    `

    you can read this to learn more about them:

    http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-expressions

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

Sidebar

Related Questions

I have two entities say Customer and Order which exist on their own and
I have two entities: Patient and Job. Patient has a to-many relationship to Job
I have two entities: product and tag connected by many-to-many relation. Here is a
i have two entities named Parent and Child , linked in a one-to-many relationship.
I have two entities, Customer and Order, both of which I have created types
I have core data model with two entities that have a many-to-many relationship with
I have two entities, Job and Language , in a many-to-one relationship. The mapping
I have parent child relationship between two entities(Parent and Child). My Parent mapping is
I have two entities, Users and Books with a has_and_belongs_to_many relationship. How can I
I have two entities, Entity1 and Entity2, on a one to many relationship. On

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.