0

Im facing a little problem here.

I have two entities: Parent and Child, Parent has a List annotated @OneToMany.

The problem is when I try to insert a new Parent, it crashes when persisting the children, because the Parent Id was not generated yet.

Is that a fix for it?

@Entity
@Table(name = "PRODUTO")
public class Parent extends BaseEntity
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID_PRODUTO")
    private Integer produtoId;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "produtoId", orphanRemoval = true)
    // @JoinTable(name = "PRODUTO_TAMANHO", joinColumns = @JoinColumn(name = "ID_PRODUTO"))
    @OrderBy("preco ASC")
    private List<Child> children;
}


@Entity
@IdClass(Child.PrimaryKey.class)
@Table(name = "PRODUTO_TAMANHO")
public class Child extends BaseEntity
{
    public static class PrimaryKey extends BaseEntity
    {
        private static final long serialVersionUID = -2697749220510151526L;

        private Integer parentId;

        private String tamanho;

        //rest of implementation
    }

    @Id
    @Column(name = "ID_PRODUTO")
    private Integer parentId;

    @Id
    @Column(name = "TAMANHO")
    private String tamanho;

    @ManyToOne
    @JoinColumn(name = "ID_PRODUTO", insertable = false, updatable = false)
    private Parent parent;
}

I think if I persist firstly the parent, than persist the children would be a bad approach.

Is that a way to persist the children, when persisting Parent?

Thanks!


Guys, the exception that occurs when persisting Parent is: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'ID_PRODUTO' cannot be null


I found a guy facing the same problem: @OneToMany and composite primary keys? (maybe it's better explained)


Here is my insertion code

Parent parent = new Parent();

Child child1 = new Child();
child1.setTamanho("Tamanho 1");
child1.setParent(parent);

Child child2 = new Child();
child2.setTamanho("Tamanho 1");
child2.setParent(parent);

List<Child> children = parent.getChildren();
children.add(child1);
children.add(child2);

save(parent);
//all of this instances, is coming from a view.jsp binded by spring, I can confirm it is exactly like this, with parentId as null
//when updating, it goes perfectly
Community
  • 1
  • 1
Davi Arimateia
  • 717
  • 6
  • 15
  • Please don't just say it "crashes". I can assure you it does not crash (i.e. quit the VM!). It probably throws an exception, and it would be helpful to know what that exception is. – dcsohl Jan 28 '15 at 17:10
  • Why is your join column not insertable and not updatable? You're effectively preventing Hibernate to insert anything in this column. Why do you have a parentId field, since you already have the parent itself? – JB Nizet Jan 28 '15 at 17:18
  • That's not how you do it. Read the hibernate documentation and search for "MapsId". Or better, stop using composite IDs. Use a single-column auto-generated ID. – JB Nizet Jan 28 '15 at 17:28
  • That is because of spring repository, as i have a composite primary key, I had to use it as Integer. About that @JoinColumn not updatable and insertable is because I already have defined it on Integer parentId, and hibernate forces it, as we have 2 same columns, on same Entity this way works perfectly when "updating" but not "saving" – Davi Arimateia Jan 28 '15 at 17:33
  • Are you setting the parent `child.setParent(parent)` and adding the child to the children list `parent.getChildren().add(child)`? – Josue Montano Jan 28 '15 at 17:51
  • @JosueMontano yes! Im setting and children, but im not setting Child parentId because it does not exists yet - Im just setting Child parent – Davi Arimateia Jan 28 '15 at 17:59

1 Answers1

0

There are few problems with your entity class.

  1. mappedBy attribute in Parent entity should be set to parent: mappedBy="parent".
  2. In child entity, below field is not required.

    @Id

    @Column(name = "ID_PRODUTO", nullable = true)

    private Integer parentId;

Updated entity is like this.

@Entity
@Table(name = "PRODUTO")
public class Parent extends BaseEntity
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID_PRODUTO")
    private Integer produtoId;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "parent", orphanRemoval = true)
    // @JoinTable(name = "PRODUTO_TAMANHO", joinColumns = @JoinColumn(name = "ID_PRODUTO"))
    @OrderBy("preco ASC")
    private List<Child> children;
}


@Entity
@IdClass(Child.PrimaryKey.class)
@Table(name = "PRODUTO_TAMANHO")
public class Child extends BaseEntity
{
    public static class PrimaryKey extends BaseEntity
    {
        private static final long serialVersionUID = -2697749220510151526L;

        private Integer parentId;

        private String tamanho;

        //rest of implementation
    }

   /* @Id
    @Column(name = "ID_PRODUTO", nullable = true)
    private Integer parentId; */ // Not required.

    @Id
    @Column(name = "TAMANHO")
    private String tamanho;

    @ManyToOne
    @JoinColumn(name = "ID_PRODUTO", insertable = false, updatable = false)
    private Parent parent;
}

Also I do not understand child inner class for primary key. Use proper primary as you have used parent.

And while inserting set both parent to child and child to parent. See my blog for more details.Here

javafan
  • 1,525
  • 3
  • 21
  • 40
  • Thanks for helping me! I tried change the mapped by to "parent" and it didnt work. Id Column(name = "ID_PRODUTO", nullable = true) private Integer parentId; Because it's a composite key, I cannot use Id tamanho as the only key, and private Parent parent is for business purposes – Davi Arimateia Jan 28 '15 at 18:33
  • Sure, im going to edit this question and post it on there! just give me a minute – Davi Arimateia Jan 28 '15 at 18:49
  • are you not getting NPE at children.add(child1); line? – javafan Jan 28 '15 at 19:56
  • nop, im getting contraint violation exception when save(parent), apparently children parentId is null (not generated) because parent is not persisted yet – Davi Arimateia Jan 28 '15 at 20:08