Diver
Diver

Reputation: 1608

How to override the default generated SQL name in @JoinColumn for a single column join

I have a unidirectional onetomany object in my entity

@OneToMany(mappedBy="pub", cascade = CascadeType.ALL, orphanRemoval=true)
@JoinColumn(name="reference")

the FK and PK columns for this entity and the child entity are both "reference"

When the SQL is generated for this join it uses the default of objectname + _ + PK "pub_reference" and ignores the name I passed in.

I can change the name= to anything and it is always ignored.

How do I override the default behavior of name even though it is a single column join?

Parent entity

@OneToMany(mappedBy="pub", cascade = CascadeType.ALL, orphanRemoval=true)
@JoinColumn(name="bob")
private Collection<PubDisplayText> pubDisplayText;

Child entity

private Pub pub;

@Id
@XmlTransient
    public Pub getPub() {
    return pub;
}

public void setPub(Pub pub) {
    this.pub = pub;
}

reference http://docs.oracle.com/javaee/6/api/javax/persistence/JoinColumn.html#name%28%29

Upvotes: 1

Views: 161

Answers (1)

Brian Vosburgh
Brian Vosburgh

Reputation: 3276

Unidirectional one-to-many mappings must use a join table. You are implying a bidirectional one-to-many by specifying the @OneToMany.mappedBy element. As a result the mapping takes the join column name from the "owning" mapping (the mapping specified by "mappedBy"). Since none is specified there, the default join column is used.

Upvotes: 1

Related Questions