What happens in a SQL database when we use nested JoinColumn in entity

Viewed 121

I am going through some code and it uses nested @JoinColumn for one of the associations, as following:

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumns({@JoinColumn(name = "QuestId"), @JoinColumn(name = "GuildId")})
private GuildQuests guildQuest;

I understand that when I do something like the following:

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumns(name="GuildQuestId")
private GuildQuests guildQuest;

I am creating a column with the name GuildQuestId in the table and it has a foreign key constraint with the entity GuildQuests.

However, what will the nested @JoinColumn(first code snippet) do?

1 Answers

Your first snippet is saying that you specify multiple foreign keys for the same association.

Example:

@ManyToOne
@JoinColumns({
    @JoinColumn(name="ADDR_ID", referencedColumnName="ID"),
    @JoinColumn(name="ADDR_ZIP", referencedColumnName="ZIP")
})
public Address getAddress() { return address; }

Edit: You're actually not creating any foreign keys with (both example) in the database. If you use for example Hibernate, Hibernate does not create any foreign keys on database level itself.

Related