[Background] I have a legacy database with following tables:
PERSON(
id VARCHAR(10) PRIMARY_KEY,
name VARCHAR(10),
surname VARCHAR(10)
)
DOG(
id VARCHAR(1) PRIMARY_KEY,
)
PERSON_DOG(
person_name VARCHAR(10), //no constraints
person_surname VARCHAR(10), //no constraints
dog_id VARCHAR(1) //no constraints
)
Now I map Dog class like this:
@Entity
@Table(name = "DOG")
class Dog{
@Id
@Column(name = "id", nullable = false)
Character id;
}
and a Person
@Entity
@Table(name = "PERSON")
class Dog{
@Id
@Column(name = "id", nullable = false)
String id;
@NotNull
@Column(name = "name", nullable = false)
String name;
@NotNull
@Column(name = "name", nullable = false)
String surname;
}
[The problem] When I try to map the Dogs collection like this:
@ManyToMany
@JoinTable(name = "PERSON_DOG",
joinColumns = {
@JoinColumn(name = "person_name", referencedColumnName = "name"),
@JoinColumn(name = "person_surname", referencedColumnName = "surname")
},inverseJoinColumns = {
@JoinColumn(name = "dog_id")
}
)
private Set<Dog> dogs;
Everything works fine. Same with this one:
@Column(name = "dog_id", nullable = true, insertable = false, updatable = false)
@ElementCollection
@CollectionTable(name = "PERSON_DOG",
joinColumns = {
@JoinColumn(name = "person_name", referencedColumnName = "name"),
@JoinColumn(name = "person_surname", referencedColumnName = "surname")
}
)
private Set<Character> dogIds;
But I know that there is only one dog for each person, so I try to map the relation like this:
@ManyToOne
@JoinTable(name = "PERSON_DOG",
joinColumns = {
@JoinColumn(name = "person_name", referencedColumnName = "name"),
@JoinColumn(name = "person_surname", referencedColumnName = "surname")
},inverseJoinColumns = {
@JoinColumn(name = "dog_id")
}
)
private Dog dog;
Then when Hibernate starts, I get this (not realy meaningful) exception:
I don't even use "SecondaryTable", so I have no idea what is going on, but it gets even better! When I try to map it like this:
@Column(name = "dog_id", nullable = true, insertable = false, updatable = false)
@JoinTable(name = "PERSON_DOG",
joinColumns = {
@JoinColumn(name = "person_name", referencedColumnName = "name"),
@JoinColumn(name = "person_surname", referencedColumnName = "surname")
}
)
private Character dogId;
The Hibernate starts correctly, but when I try to load the person I get this exception:
Because Hibernate create query like this:
|