C'è un esempio che è completamente simile al tuo caso nello Hibernate reference documentation. Poco prima di questo esempio, troverai le spiegazioni. Ecco l'esempio, che corrisponde al tuo problema (Utente tabella A, e il cliente è la tabella B):
@Entity
class Customer {
@EmbeddedId CustomerId id;
boolean preferredCustomer;
@MapsId("userId")
@JoinColumns({
@JoinColumn(name="userfirstname_fk", referencedColumnName="firstName"),
@JoinColumn(name="userlastname_fk", referencedColumnName="lastName")
})
@OneToOne User user;
}
@Embeddable
class CustomerId implements Serializable {
UserId userId;
String customerNumber;
//implements equals and hashCode
}
@Entity
class User {
@EmbeddedId UserId id;
Integer age;
}
@Embeddable
class UserId implements Serializable {
String firstName;
String lastName;
//implements equals and hashCode
}
Nota: sarebbe molto più semplice di voi hanno avuto un identificatore surrogato per quei due tabelle. A meno che non sei costretto a gestire uno schema precedente, fai un favore a te stesso e usa le chiavi surrogate.
fonte
2011-08-22 11:49:35
Ma sai se ci sono dei problemi se cambio invece di mettere l'annotazione OneToOne sul Cliente, metti OneToMany sulla classe User ??? – rascio
Dovrai renderlo un'associazione OneToMany/ManyToOne bidirezionale. –
Così facendo, ottengo "Attributo" userId "ha un mapping non valido in questo contesto" all'interno di "CustomerId" –