JPA-mapping @ManyToOne zwischen Integrierbaren und EmbeddedId
Habe ich Folgendes setup in meinem Spring-Boot-JPA-Anwendung:
Embeddable
@Embeddable
public class LogSearchHistoryAttrPK {
@Column(name = "SEARCH_HISTORY_ID")
private Integer searchHistoryId;
@Column(name = "ATTR", length = 50)
private String attr;
@ManyToOne
@JoinColumn(name = "ID")
private LogSearchHistory logSearchHistory;
...
}
EmbeddedId
@Repository
@Transactional
@Entity
@Table(name = "LOG_SEARCH_HISTORY_ATTR")
public class LogSearchHistoryAttr implements Serializable {
@EmbeddedId
private LogSearchHistoryAttrPK primaryKey;
@Column(name = "VALUE", length = 100)
private String value;
...
}
OneToMany
@Repository
@Transactional
@Entity
@Table(name = "LOG_SEARCH_HISTORY")
public class LogSearchHistory implements Serializable {
@Id
@Column(name = "ID", unique = true, nullable = false)
private Integer id;
@OneToMany(mappedBy = "logSearchHistory", fetch = FetchType.EAGER)
private List<LogSearchHistoryAttr> logSearchHistoryAttrs;
...
}
Datenbank-DDLs
CREATE TABLE log_search_history (
id serial NOT NULL,
...
CONSTRAINT log_search_history_pk PRIMARY KEY (id)
);
CREATE TABLE log_search_history_attr (
search_history_id INTEGER NOT NULL,
attr CHARACTER VARYING(50) NOT NULL,
value CHARACTER VARYING(100),
CONSTRAINT log_search_history_attr_pk PRIMARY KEY (search_history_id, attr),
CONSTRAINT log_search_history_attr_fk1 FOREIGN KEY (search_history_id) REFERENCES
log_search_history (id)
);
Wenn ich gehen, um die Anwendung zu starten, bekomme ich die folgende Fehlermeldung:
Verursacht durch: org.hibernate.AnnotationException: mappedBy eine Referenz zu einem unbekannten Ziel-Entität Eigenschaft: com.foobar.Entität.LogSearchHistoryAttr.logSearchHistory in com.foobar.Entität.LogSearchHistory.logSearchHistoryAttrs
Ich bin nicht sicher, warum ich diese Fehlermeldung bekommen - das mapping korrekt aussieht (zu mir). Was ist falsch mit dieser Zuordnung, die ich habe? Danke!
- wo ist
logSearchHistory
imLogSearchHistoryAttr
? - es ist in der EmbeddedId Objekt. Sollte es gezogen werden von dort aus und in die
logSearchHistoryAttr
? - Manuell hinzufügen targetEntity zu Ihrem OneToMany-annotation.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Bewegten sich die
mappedBy
Attribut in eine einbettbare Primärschlüssel, also das Feld ist nicht mehr benanntlogSearchHistory
sondernprimaryKey.logSearchHistory
. Ändern Sie Ihr zugeordnet Eintrag:Referenz: JPA /Hibernate OneToMany Zuordnung, die mit einem zusammengesetzten Primärschlüssel.
Sie auch brauchen, um die primary key Klasse
LogSearchHistoryAttrPK
serializable.@ManyToOne
mapping? Ich Frage weil ich bekomme die Fehlermeldung: >FEHLER: Spalte logsearchh8_.id existiert nichtIn OneToMany Teil: