Hibernate version: Hibernate Core 3.2.4.SP1, Hibernate Annotations 3.3.0 GA
Name and version of the database: MySQL 5.0
Bisher habe ich das Mapping mit XML realisiert. Jetzt habe ich zu Annotations gewechselt und habe das Problem, dass ich nun bei den Referenzen die Wahl zwischen dem kompletten Objekt oder NULL habe. Beim Mapping mit XML hatte ich über den fetch type "lazy" die Datenbank-IDs der Objekte und konnte mit ihnen neue Abfragen starten, wenn nötig.
Da ich aber durch ein umfangreiches Datenmodell mit vielen Einträgen nicht alles auf FetchType.EAGER setzen kann, brauche ich eine Lösung mit dem PROXY. Laut Dokumentation sollte genau das der Fall sein, wenn man die referenzierten Objekte mit dem FetchType.LAZY versieht. Doch leider sind die Objekte immer NULL.
Meine Mapping Klassen mit Annotations:
Code:
@Entity
@Table(name = "user")
@org.hibernate.annotations.Proxy(lazy = true)
public class User implements java.io.Serializable {
private Integer id;
private String login;
private String password;
private Set<Fileright> filerights = new HashSet<Fileright>(0);
private Map<Rights, File> rightsAndFile = new HashMap<Rights, File>(0);
private int hashValue = 0;
public User() {
}
@Id
@Column(name = "id")
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "login")
@NotNull
public String getLogin() {
return this.login;
}
public void setLogin(String login) {
this.login = login;
}
@Column(name = "password")
@NotNull
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
public Set<Fileright> getFilerights() {
return filerights;
}
public void setFilerights(Set<Fileright> filerights) {
this.filerights = filerights;
}
@ManyToMany(fetch = FetchType.LAZY)
@MapKeyManyToMany(joinColumns = @JoinColumn(name = "rightid"))
@JoinTable(name = "fileright",
joinColumns = {@JoinColumn(name = "userid")}, inverseJoinColumns = {@JoinColumn(name = "fileid")})
public Map<Rights, File> getRightsAndFile() {
return rightsAndFile;
}
public void setRightsAndFile(Map<Rights, File> rightsAndFile) {
this.rightsAndFile = rightsAndFile;
}
}
@Entity
@Table(name = "fileright")
@org.hibernate.annotations.Proxy(lazy = true)
public class Fileright implements java.io.Serializable {
private Integer id;
private User user;
private Rights rights;
private File file;
private int hashValue = 0;
/** default constructor */
public Fileright() {
}
@Id
@Column(name = "id")
@GeneratedValue(strategy=GenerationType.AUTO)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "userid", nullable = false)
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "rightid", nullable = false)
public Rights getRights() {
return this.rights;
}
public void setRights(Rights rights) {
this.rights = rights;
}
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "fileid", nullable = false)
public File getFile() {
return this.file;
}
public void setFile(File file) {
this.file = file;
}
}
Das alte, funktionierende XML Mapping Code:
- <hibernate-mapping default-cascade="none" default-access="property" default-lazy="true" auto-import="true">
- <class name="de.partners.la.hibernate.User" table="user" catalog="la" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false" select-before-update="false" optimistic-lock="version">
- <id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
- <property name="login" type="java.lang.String" unique="false" optimistic-lock="true" lazy="false" generated="never">
<column name="login" not-null="true" />
</property>
- <property name="password" type="java.lang.String" unique="false" optimistic-lock="true" lazy="false" generated="never">
<column name="password" not-null="true" />
</property>
- <!-- ====================== one-to-many References =========================
-->
- <set name="filerightSet" inverse="true" sort="unsorted" mutable="true" optimistic-lock="true" embed-xml="true">
- <key on-delete="noaction">
<column name="userid" not-null="true" />
</key>
<one-to-many class="de.partners.la.hibernate.Fileright" not-found="exception" embed-xml="true" />
</set>
- <set name="projectuserSet" inverse="true" sort="unsorted" mutable="true" optimistic-lock="true" embed-xml="true">
- <key on-delete="noaction">
<column name="userid" not-null="true" />
</key>
<one-to-many class="de.partners.la.hibernate.Projectuser" not-found="exception" embed-xml="true" />
</set>
- <!-- ====================== many-to-many References =========================
-->
- <set name="rights" table="fileright" inverse="true" sort="unsorted" mutable="true" optimistic-lock="true" embed-xml="true">
<key column="userid" not-null="true" on-delete="noaction" />
<many-to-many column="rightid" class="de.partners.la.hibernate.Rights" embed-xml="true" not-found="exception" unique="false" />
</set>
</class>
</hibernate-mapping>
- <hibernate-mapping default-cascade="none" default-access="property" default-lazy="true" auto-import="true">
- <class name="de.partners.la.hibernate.Fileright" table="fileright" catalog="la" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false" select-before-update="false" optimistic-lock="version">
- <id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
- <many-to-one name="user" class="de.partners.la.hibernate.User" fetch="select" unique="false" update="true" insert="true" optimistic-lock="true" not-found="exception" embed-xml="true">
<column name="userid" not-null="true" />
</many-to-one>
- <many-to-one name="rights" class="de.partners.la.hibernate.Rights" fetch="select" unique="false" update="true" insert="true" optimistic-lock="true" not-found="exception" embed-xml="true">
<column name="rightid" not-null="true" />
</many-to-one>
- <many-to-one name="file" class="de.partners.la.hibernate.File" fetch="select" unique="false" update="true" insert="true" optimistic-lock="true" not-found="exception" embed-xml="true">
<column name="fileid" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
Freue mich auf jeden konkreten Hinweis.
Vielen Dank schon mal,
Markus