Hibernate OneToMany

I

i125

Hallo zusammen,

vorab, ich bin absoluter Java Neuling... ich hab da ein kleines Problemchen mit Hibernate:

Es soll eine Relation zwischen den Tabellen USER und GROUP abgebildet werden.

Die Tabellen sehen so aus:

Tabelle User:
id PK
active
firstname
lastname
emailaddress
username
password
fk_group_id (FK von der Tabelle group)

Die Tabelle Group:
id OK
name
permission

.... Im Java siehts bei mir dann so aus:

Code:
package test.tables;

import java.io.Serializable;
import java.util.HashSet;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.mapping.Set;

@Entity
@Table(name = "USER")
public class USER implements Serializable{
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Integer id;
	private Boolean active;
	private String firstname;
	private String lastname;
	private String emailaddress;
	private String username;
	@OneToMany(mappedBy="fk_group_id")
	private Set<fk_group_id> fk_group_id = new HashSet<fk_group_id>();
	
	public static long getSerialVersionUID() {
		return serialVersionUID;
	}
	public Boolean getActive() {
		return active;
	}
	public void setActive(Boolean active) {
		this.active = active;
	}
	public String getEmailaddress() {
		return emailaddress;
	}
	public void setEmailaddress(String emailaddress) {
		this.emailaddress = emailaddress;
	}
	public String getFirstname() {
		return firstname;
	}
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}
	public Set<fk_group_id> getFk_group_id() {
		return fk_group_id;
	}
	public void setFk_group_id(Set<fk_group_id> fk_group_id) {
		this.fk_group_id = fk_group_id;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getLastname() {
		return lastname;
	}
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}

	

}

Code:
package test.tables;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "GROUP")
public class GROUP implements Serializable{
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@ManyToOne
	@JoinColumn
	private Integer id;
	private String name;
	private String permission;
	
	public static long getSerialVersionUID() {
		return serialVersionUID;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPermission() {
		return permission;
	}
	public void setPermission(String permission) {
		this.permission = permission;
	}
}

Leider funktioniert das nicht wirklich. Mein Fragen dazu:
In der Tabelle, wo der FK Key eingetragen wird, muss doch

@OneToMany(mappedBy="KEY? Tabellename?")
private Set<fk_group_id> fk_group_id = new HashSet<fk_group_id>();

Eclipse meckert nun an: fk_group_id? Muss da das Objekt hin?

Grüße

Alex
 
Wenn du Java-Neuling bist, solltest vielleicht nicht sofort mit Hibernate starten, sondern einige Tutorials für Einsteiger durcharbeiten.
Entsprechende Links findest hier in den dazugehörigen Foren.

Ansonsten hier mal Beispiele von mir:

Benutzer zu Gruppe ist ManyToMany
Gruppe zu Projekt ist ManyToOne bzw OneToMany

die Kreuztabelle zwischen Benutzer und Gruppe (m:n) musst du in Hibernate nicht extra anlegen, in der DB sollte sie allerdings vorhanden sein.

User:
Java:
package com.ac.stdv.model;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "user", schema = "std_verwaltung")
public class User implements Serializable{

	private static final long serialVersionUID = 1L;

	@Id
	@Column(name = "id", unique = true, nullable = false, insertable = true, updatable = false, precision = 22, scale = 0)
	private int id;

	@Column(name = "name", unique = true, nullable = false, insertable = true, updatable = false, length = 20)
	private String name;

	@Column(name = "pw", unique = false, nullable = false, insertable = true, updatable = true, length = 20)
	private String pw;

	@Column(name = "level", unique = false, nullable = false, insertable = true, updatable = true)
	private int level;
	
	@Column(name = "aktiv", unique = false, nullable = false, insertable = true, updatable = true)
	private int aktiv;

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "User_Gruppe", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "gruppe_id") })
	private Set<Gruppe> gruppen;
	
	@OneToMany(fetch = FetchType.LAZY, mappedBy="user")
	@JoinColumn(name = "user_id")
	private Set<Arbeitsstunden> stunden;
	
	public Set<Arbeitsstunden> getStunden() {
		return stunden;
	}

	public void setStunden(Set<Arbeitsstunden> stunden) {
		this.stunden = stunden;
	}

	public Set<Gruppe> getGruppen() {
		return gruppen;
	}

	public void setGruppen(Set<Gruppe> gruppen) {
		this.gruppen = gruppen;
	}

	public int getBenutzer_id() {
		return id;
	}

	public void setBenutzer_id(int id) {
		this.id = id;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPw() {
		return pw;
	}

	public void setPw(String pw) {
		this.pw = pw;
	}

	public int getAktiv() {
		return aktiv;
	}

	public void setAktiv(int aktiv) {
		this.aktiv = aktiv;
	}
}

Gruppe:
Java:
package com.ac.stdv.model;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "gruppe", schema = "std_verwaltung")
public class Gruppe implements Serializable {

	private static final long serialVersionUID = 1L;
	@Id
	@Column(name = "id", unique = true, nullable = false, insertable = true, updatable = false, precision = 22, scale = 0)
	private int id;

	@Column(name = "aktiv", unique = false, nullable = false, insertable = true, updatable = true, precision = 22, scale = 0)
	private int aktiv;

	@Column(name = "name", unique = true, nullable = false, insertable = true, updatable = false, length = 20)
	private String name;

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "User_Gruppe", joinColumns = { @JoinColumn(name = "gruppe_id") }, inverseJoinColumns = { @JoinColumn(name = "user_id") })
	private Set<User> user;

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "gruppe")
	private Set<Projekt> projekte;

	public Set<Projekt> getProjekte() {
		return projekte;
	}

	public void setProjekte(Set<Projekt> projekte) {
		this.projekte = projekte;
	}

	public int getId() {
		return id;
	}

	public void setId(int gruppe_id) {
		this.id = gruppe_id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set<User> getUser() {
		return user;
	}

	public void setUser(Set<User> user) {
		this.user = user;
	}

	public int getAktiv() {
		return aktiv;
	}

	public void setAktiv(int aktiv) {
		this.aktiv = aktiv;
	}
}

Projekt:
Java:
package com.ac.stdv.model;

import java.io.Serializable;
import java.sql.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "projekt", schema = "std_verwaltung")
public class Projekt implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@Column(name = "id", unique = true, nullable = false, insertable = true, updatable = false, precision = 22, scale = 0)
	private int id;

	@ManyToOne(cascade = {}, fetch = FetchType.EAGER)
	@JoinColumn(name = "gruppe_id", unique = false, nullable = false, insertable = true, updatable = true)
	private Gruppe gruppe;

	@Column(name = "name", unique = true, nullable = false, insertable = true, updatable = true, length = 30)
	private String name;

	@Column(name = "start", unique = false, nullable = false, insertable = true, updatable = true)
	private Date start;

	@Column(name = "ende", unique = false, nullable = false, insertable = true, updatable = true)
	private Date ende;

	@Column(name = "sollstd", unique = false, nullable = false, insertable = true, updatable = true)
	private int soll_stunden;

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy="projekt")
	@JoinColumn(name = "projekt_id")
	private Set<Arbeitsstunden> stunden;

	public Set<Arbeitsstunden> getStunden() {
		return stunden;
	}

	public void setStunden(Set<Arbeitsstunden> stunden) {
		this.stunden = stunden;
	}

	public Date getEnde() {
		return ende;
	}

	public void setEnde(Date ende) {
		this.ende = ende;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getSoll_stunden() {
		return soll_stunden;
	}

	public void setSoll_stunden(int soll_stunden) {
		this.soll_stunden = soll_stunden;
	}

	public Date getStart() {
		return start;
	}

	public void setStart(Date start) {
		this.start = start;
	}

	public Gruppe getGruppe() {
		return gruppe;
	}

	public void setGruppe(Gruppe gruppe) {
		this.gruppe = gruppe;
	}

}
 
Zuletzt bearbeitet:
Zurück