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

  • Home
  • SEARCH
  • 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 6093191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:31:55+00:00 2026-05-23T12:31:55+00:00

I have the following Object: public class Constraint { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) int id; String

  • 0

I have the following Object:

public class Constraint {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
int id;

String name;
String description;
String path;
int level;

@ManyToOne
@JoinColumn(name="parent_id")
Constraint parent;

@OneToMany
@JoinColumn(name="parent_id")
Set<Constraint> children;

@ManyToOne
@JoinColumn(name="type_id")
ConstraintType type; }

As you see this table represents a tree structure. Having the functions:

public List<Constraint> getAllDescendantsOfConstraint(Integer id) {
    StringBuilder sb = new StringBuilder(); 
    sb.append("WITH RECURSIVE tree as "); 
    sb.append("( "); 
    sb.append(" select * from constraints where id = :id "); 
    sb.append(" union all "); 
    sb.append(" select a.* from constraints a, tree b where a.parent_id = b.id "); 
    sb.append(") "); 
    sb.append("select * from tree where id <> :id ");

    Query q = entityManager.createNativeQuery(sb.toString(), Constraint.class);
    q.setParameter("id", id);
    return (List<Constraint>) q.getResultList();
}
@Transactional
public void setupPaths() {
    Query q = entityManager.createQuery("from Constraint", Constraint.class);
    @SuppressWarnings("unchecked")
    List<Constraint> constraints = (List<Constraint>) q.getResultList();

    for(Constraint c : constraints) {
        List<Constraint> descendants = getAllDescendantsOfConstraint(c.getId());
        for(Constraint d : descendants) {
            String tpath;
            if((tpath = d.getPath()) == null || d.getPath().equals(""))
                tpath = String.valueOf(c.getId());
            else
                tpath = d.getPath() + "." + String.valueOf(c.getId());
            d.setPath(tpath);
            entityManager.persist(d);
        }
    }
}

If a call setupPaths i get a “detached entity passed to persist exception”.
If instead of entityManager.persist(d); i do :

d = entityManager.merge(d);
            d.setPath(tpath);

Nothing happens (the data is not saved in the db). If a call flush, i get detached entity passed to persist exception as well. I suspect it has something to do with me not eagerly loading the parent or children?

edit: After further testing since the above seems really fishy, it seems that every entity i get from entityManager, no matter how i get it (either with em.find(MyObject.class,id or any kind of queries) it is detached right after fetching! I see that from calling

em.contains(myObject)

which always returns false. The problem is that i use the exact same setup on other spring projects with the same database server and it’s wroking fine.

  • 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-23T12:31:55+00:00Added an answer on May 23, 2026 at 12:31 pm

    After (finally) turning debug info for Hibernate on i saw that the EntityManager was closing the session right after fetch which was wrong since i was using @Transactional annotation. Finally it seems that was a problem with STS (springsource toolsuite) and spring configuration, particularly:

    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
    

    This supposedly witht the correct maven setup

    <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.3.1</version>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjrt</artifactId>
                        <version>${org.aspectj-version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${org.aspectj-version}</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>compile</id>
                        <configuration>
                            <source>${java-version}</source>
                            <target>${java-version}</target>
                            <verbose>false</verbose>
                            <outxml>true</outxml>
                            <aspectLibraries>
                                <aspectLibrary>
                                    <groupId>org.springframework</groupId>
                                    <artifactId>spring-aspects</artifactId>
                                </aspectLibrary>
                            </aspectLibraries>
                        </configuration>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        </execution>
                </executions>
                <configuration>
                    <outxml>true</outxml>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                </configuration>
            </plugin>
    

    should take care of compile-time weaving but something went wrong. I was using plugin 1.2, when i changed that to 1.3.1 it magically worked.

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

Sidebar

Related Questions

Let's say I have the following class: public class Test<E> { public boolean sameClassAs(Object
So let's say we have a domain object such as the following public class
I have the following class objects: public class VacancyCategory { public int ID {
I have the following object: public partial class Game { public bool Finished {
I have the following code: public class Tests { public static void main(String[] args)
i have the following entities: public class Category { public virtual int CategoryID {
I want to have the following setup: abstract class Parent { public static String
i have the following classes: //Product class public class Product { @Id @GeneratedValue private
I have the following collection contract defined: [CollectionDataContract(Name = Centres)] public class Centres :
I have the following example code: class A(object): def __init__(self, id): self.myid = id

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.