0

I have an Entity which contains a collection of another Entity which itself references the same class in a collection, with the following mappings:

@Entity
@Table(name="parents")
public class Parent {
    @Id
    @GeneratedValue
    private long id;
    @ElementCollection
    @Cascade(org.hibernate.annotations.CascadeType.ALL)
    private Set<Child> children;

    public Parent() {}

    public Parent(Set<Child> children) {
        this.children = children;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Set<Child> getChildren() {
        return children;
    }

    public void setChildren(Set<Child> children) {
        this.children = children;
    }

    @Entity
    @Table(name="children")
    public static class Child {
        @Id
        @GeneratedValue
        private long id;
        private String name;
        @ElementCollection
        @Cascade(org.hibernate.annotations.CascadeType.ALL)
        private Set<Child> dependencies;

        public Child() {}

        public Child(String name) {
            this.name = name;
            dependencies = new LinkedHashSet<>();
        }

        public long getId() {
            return id;
        }

        public void setId(long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public getDependencies() {
            return dependencies;
        }

        public setDependencies(Set<Child> dependencies) {
            this.dependencies = dependencies;
        }
    }
}

But when I try to save it, I get a: java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing exception

I've read in another post that I should use @Cascade(CascadeType.ALL) annotation, but as you can see, I tried doing so and it didn't solve my problem.

Please help me understand what's wrong with these mappings

Ordinaly
  • 69
  • 1
  • 9
  • Your mappings are all wrong. You need to be using `@OneToMany` rather than `@ElementCollection` as this only used for collections of simple types or embeddables: not entities. https://stackoverflow.com/questions/8969059/difference-between-onetomany-and-elementcollection – Alan Hay Jul 11 '19 at 12:44
  • Pardon my ignorance, I am just getting started with JPA, I have changed my annotations to `@OneToMany(cascade = CascadeType.ALL)` and now I get `org.springframework.orm.jpa.JpaSystemException: could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement ` – Ordinaly Jul 11 '19 at 12:58
  • There will be more to the message than that. – Alan Hay Jul 11 '19 at 13:00
  • SQL said: "Field 'id' doesn't have a default value" – Ordinaly Jul 11 '19 at 13:04
  • You need to specify a strategy https://docs.oracle.com/javaee/7/api/javax/persistence/GeneratedValue.html. – Alan Hay Jul 11 '19 at 13:05
  • Now I get `Duplicate entry '7' for key 'UK_q8xuxyx9iylvc9ddh8fudtlsf'` and `org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [UK_q8xuxyx9iylvc9ddh8fudtlsf]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement` – Ordinaly Jul 11 '19 at 13:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196324/discussion-between-gaussianblurs-and-alan-hay). – Ordinaly Jul 11 '19 at 13:12
  • The problem seems to be that two `Child` reference the same `Child` in their `dependencies`, how can I solve this ? – Ordinaly Jul 11 '19 at 13:38
  • Probably best to delete this question and ask a new one with your updated code. Or update this question accordingly. – Alan Hay Jul 11 '19 at 13:44
  • Thanks for your help, I will post a new question – Ordinaly Jul 11 '19 at 13:47
  • https://stackoverflow.com/questions/56990905/duplicate-entry-when-trying-to-save-entity – Ordinaly Jul 11 '19 at 13:56

0 Answers0