3

Is there no way of making the 'one' the owner side of a one-to-many relationship? Every single sample has mappedBy in @OneToMany. Even the answer to this similar question has that.

I want the one side to be the owning side because when I add a many/child, I also change some information on the one/parent, and I want just to update the parent, for code simplicity and to ensure that it all goes in the same ACID transaction.

But I just can't make it work. Hibernate keeps complaining about

Repeated column in mapping for entity

My entities:

public class Parent {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @OneToMany(fetch = EAGER, cascade = {ALL}, orphanRemoval = true)
    @JoinColumn(name = "parent_id", referencedColumnName = "id", nullable = false)
    private final Set<Child> children = new HashSet<>();

}

public class Child {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @ManyToOne
    private Parent parent;

}
Community
  • 1
  • 1
user384729
  • 403
  • 1
  • 7
  • 16

2 Answers2

1

To solve Repeated column in mapping for entity error.

You can remove the nullable=false from @JoinColumn annotation as below:

Instead of

@JoinColumn(name = "parent_id", referencedColumnName = "id", nullable = false)

try with

@JoinColumn(name = "parent_id", referencedColumnName = "id", insertable = false, updatable = false)

But if you want to retain nullable=false, then

either remove private Parent parent; declaration from the Child class

or

make it transient either using @Transient annotation or private transient Parent parent;

0

What you have

@JoinColumn(name = "parent_id", referencedColumnName = "id")

is no different from

@OneToMany(mappedBy="parent", fetch = FetchType.EAGER, cascade = {CascadeType.ALL}, orphanRemoval = true)

on the face of it. What is your motivation for doing it?

EDIT: Yes, ok. There is a difference in how you persist with a bidirectional relationship. You have to set the parent if you use mappedBy, and you don't have to set the parent if you use @JoinColumn, but only if you remove the ,insertable = false, updatable = false`, so I'm not sure why there was a discussion about having to include those. Also, I'm not sure why you are having the hibernate repeated column error, I didn't get anything like that. It should be noted that the CascadeType.ALL is required for child to be saved without having to set the parent.

K.Nicholas
  • 10,956
  • 4
  • 46
  • 66
  • This is very disturbing, I hope it's a misunderstanding; otherwise I'm very wrong. I think that what I have (your first code snippet), makes my `Parent` the owning side of the relationship. Thus, I don't need to manually persist a new `Child`. I can just add it to the parent and update the parent. With your second snippet, `Child` would be the owning side. I would need to do `child.setParent(parent)` and manually persist it, childDao.save(child)`. – user384729 Mar 08 '16 at 16:43