Hallo ich bin neu hier und lerne gerade Hibernate. Ich versuche mir eine Filmdatenbank zu bauen worin ich meine Filme verwalten kann mit Hibernate. Wir ihr euch sicher denken könnt melde ich mich hier, weil ich ein Problem habe.
Mein Problem: Ich Versuche einen Film der bereits existiert eine weitere DVD zu zuordnen, aber Hibernate überschreibt immer nur die bestehende Zuordnung anstatt eine weitere in die Tabelle einzufügen.
Ich benutze Hibernate Version 4.1.4.Final.
Das Tabellenschema sieht folgendermaßen aus:
Tabellen: tbl_Movie, tbl_MovieDVD, tbl_DVD und tbl_Box
Die Tabellen tbl_Movie und tbl_DVD werden über die Tabelle tbl_MovieDVD zugeordnet. Die Tabelle tbl_Box hat einen FK in der Tabelle tbl_DVD.
Hier die Entity Klassen:
Der Code für die Movie Klasse:
Code:
import javax.persistence.*;
@Entity
@Table(name = "tbl_Movie")
public class DO_Movie implements java.io.Serializable {
// --------------------------- Field Declaration ---------------------------
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "MovieNr")
private int movieNr;
@Column(name = "MovieName")
private String movieName;
@Column(name = "MovieSubtitleName")
private String movieSubName;
@ManyToMany
@JoinTable(name="tbl_MovieDVD",
joinColumns={@JoinColumn(name="FK_MovieNr", updatable=false, insertable=true)},
inverseJoinColumns={@JoinColumn(name="FK_DVDNr", updatable=false, insertable=true)}
)
private java.util.List<DO_DVD> dvds;
// ------------------------- Field Declaration End -------------------------
// ------------------ Function Declaration and Definition ------------------
public DO_Movie() {
}
/**
* @return the movieNr
*/
public int getMovieNr() {
return movieNr;
}
/**
* @param movieNr the movieNr to set
*/
public void setMovieNr(int movieNr) {
this.movieNr = movieNr;
}
/**
* @return the movieName
*/
public String getMovieName() {
return movieName;
}
/**
* @param movieName the movieName to set
*/
public void setMovieName(String movieName) {
this.movieName = movieName;
}
/**
* @return the movieSubName
*/
public String getMovieSubName() {
return movieSubName;
}
/**
* @param movieSubName the movieSubName to set
*/
public void setMovieSubName(String movieSubName) {
this.movieSubName = movieSubName;
}
/**
* @return the dvds
*/
public java.util.List<DO_DVD> getDvds() {
return dvds;
}
/**
* @param dvds the dvds to set
*/
public void setDvds(java.util.List<DO_DVD> dvds) {
this.dvds = dvds;
}
// ---------------- Function Declaration and Definition End ----------------
}
Der Code für die DVD Klasse:
Code:
import javax.persistence.*;
@Entity
@Table(name = "tbl_DVD")
public class DO_DVD implements java.io.Serializable {
// --------------------------- Field Declaration ---------------------------
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "DVDNr")
private int dvdNr;
@ManyToMany
@JoinTable(name="tbl_MovieDVD",
joinColumns={@JoinColumn(name="FK_DVDNr", updatable=false, insertable=true)},
inverseJoinColumns={@JoinColumn(name="FK_MovieNr", updatable=false, insertable=true)}
)
private java.util.List<DO_Movie> movies;
@Column(name = "DVDHinweis")
private String dvdHinweis;
@ManyToOne
@JoinColumn(name="FK_BoxNr", nullable=true)
private DO_Box box;
// ------------------------- Field Declaration End -------------------------
// ------------------ Function Declaration and Definition ------------------
public DO_DVD() {
}
/**
* @return the dvdNr
*/
public int getDvdNr() {
return dvdNr;
}
/**
* @param dvdNr the dvdNr to set
*/
public void setDvdNr(int dvdNr) {
this.dvdNr = dvdNr;
}
/**
* @return the movies
*/
public java.util.List<DO_Movie> getMovies() {
return movies;
}
/**
* @param movies the movies to set
*/
public void setMovies(java.util.List<DO_Movie> movies) {
this.movies = movies;
}
/**
* @return the dvdHinweis
*/
public String getDvdHinweis() {
return dvdHinweis;
}
/**
* @param dvdHinweis the dvdHinweis to set
*/
public void setDvdHinweis(String dvdHinweis) {
this.dvdHinweis = dvdHinweis;
}
public String getMovieNames() {
String ret = "";
for(int i = 0;i < this.movies.size();i++) {
ret += this.movies.get(i).getMovieName();
if(i < (this.movies.size() - 1)) {
ret += ", ";
}
}
return ret;
}
/**
* @return the box
*/
public DO_Box getBox() {
return box;
}
/**
* @param box the box to set
*/
public void setBox(DO_Box box) {
this.box = box;
}
// ---------------- Function Declaration and Definition End ----------------
}
Hier der Code der Tabelle Box
Code:
import javax.persistence.*;
@Entity
@Table(name = "tbl_Box")
public class DO_Box implements java.io.Serializable {
// --------------------------- Field Declaration ---------------------------
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "BoxNr")
private int boxNr;
@ManyToOne
@JoinColumn(name="FK_LagerplatzNr", nullable=true)
private DO_Lagerplatz lagerplatz;
@Column(name = "BoxHinweis")
private String boxHinweis;
// ------------------------- Field Declaration End -------------------------
// ------------------ Function Declaration and Definition ------------------
public DO_Box() {
}
/**
* @return the boxNr
*/
public int getBoxNr() {
return boxNr;
}
/**
* @param boxNr the boxNr to set
*/
public void setBoxNr(int boxNr) {
this.boxNr = boxNr;
}
/**
* @return the lagerplatz
*/
public DO_Lagerplatz getLagerplatz() {
return lagerplatz;
}
/**
* @param lagerplatz the lagerplatz to set
*/
public void setLagerplatz(DO_Lagerplatz lagerplatz) {
this.lagerplatz = lagerplatz;
}
/**
* @return the boxHinweis
*/
public String getBoxHinweis() {
return boxHinweis;
}
/**
* @param boxHinweis the boxHinweis to set
*/
public void setBoxHinweis(String boxHinweis) {
this.boxHinweis = boxHinweis;
}
// ---------------- Function Declaration and Definition End ----------------
}
Die Funktionen zum speichern von Movie, DVD und Box habe ich in andere Klassen ausgelagert und steuere sie nach einander an.
Die Methode die alles anlegen soll:
Code:
private void handleButtonBoxAnlegenAction(ActionEvent event) {
System.out.println("Box Anlegen");
this.box = new FilmDB.DAO.DAO_Box();
this.dvd = new FilmDB.DAO.DAO_DVD();
this.movie = new FilmDB.DAO.DAO_Movie();
// ---------------------------------------------------------------------
// erzeuge Box
DO_Box[] p = new DO_Box[1];
p[0] = new DO_Box();
p[0].setBoxHinweis(this.TextAreaBoxHinweis.getText());
String lp = this.ComboBoxLagerplatz.getSelectionModel().getSelectedItem().toString();
for(int i = 0;i < this.lp.getAnzahlElemente();i++) {
if(lp.equals(this.lp.getLagerplatzName(i)) == true) {
p[0].setLagerplatz(this.lp.getLagerplatzList().get(i));
break;
}
}
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Box anlegen
this.box.insertBox(p);
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// setze die Boxen auf die DVDs
for(int i = 0; i < this.dvds.size();i++)
this.dvds.get(i).setBox(p[0]);
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// DVDs to Array wandeln
DO_DVD[] VAL = new DO_DVD[this.dvds.size()];
this.dvds.toArray(VAL);
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Listen der Filme in Array über führen
java.util.List<DO_Movie>[] VAL2 = new java.util.List[this.dvds.size()];
for(int i = 0;i < VAL.length;i++) {
VAL2[i] = VAL[i].getMovies();
VAL[i].setMovies(null);
}
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// DVDs anlegen
this.dvd.insertDVD(VAL);
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
java.util.List<DO_Movie> movielist = null;
java.util.ArrayList<DO_DVD> dvdlist = null;
DO_Movie[] VAL1 = null;
DO_Movie[] VAL3 = null;
this.dvds.toArray(VAL);
for(int i = 0;i < VAL2.length;i++) {
movielist = VAL2[i];
VAL1 = new DO_Movie[movielist.size()];
movielist.toArray(VAL1);
VAL3 = this.movie.insertMovies(VAL1);
dvdlist = new java.util.ArrayList<>();
dvdlist.add(VAL[i]);
dvdlist.trimToSize();
for(int j = 0;j < VAL3.length;j++) {
if(VAL3[j].getDvds() != null) {
VAL3[j].setDvds(dvdlist);
this.movie.addNewDVDToExistingMovie(VAL3[j], VAL[i]);
} else {
VAL3[j].setDvds(dvdlist);
this.movie.updateMovies(VAL3[j]);
}
}
}
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
this.dvds = FXCollections.observableArrayList();
this.TableDVDs.setItems(this.dvds);
// ---------------------------------------------------------------------
this.TextFieldFilmName.requestFocus();
}
Und hier der Code der aufgerufenen Funktionen ihrer aufruf Reihenfolge nach:
Code:
public void insertBox(DO_Box[] BOX) {
SessionFactory sf = null;
Session session = null;
Transaction transaction = null;
try {
sf = HibernateUtil.configureSessionFactoryAnnotation();
session = sf.getCurrentSession();
transaction = session.beginTransaction();
for(int i = 0;i < BOX.length;i++) {
session.persist(BOX[i]);
}
transaction.commit();
} catch(Exception ex) {
try {
transaction.rollback();
} catch(Exception ex1) {
ex1.printStackTrace();
}
ex.printStackTrace();
}
}
Code:
public void insertDVD(DO_DVD[] DVD) {
SessionFactory sf = null;
Session session = null;
Transaction transaction = null;
try {
sf = HibernateUtil.configureSessionFactoryAnnotation();
session = sf.getCurrentSession();
transaction = session.beginTransaction();
for(int i = 0;i < DVD.length;i++) {
session.persist(DVD[i]);
}
transaction.commit();
} catch(Exception ex) {
try {
transaction.rollback();
} catch(Exception ex1) {
ex1.printStackTrace();
}
ex.printStackTrace();
}
}
Code:
public DO_Movie[] insertMovies(DO_Movie[] MOVIE) {
SessionFactory sf = null;
Session session = null;
Transaction transaction = null;
Query query = null;
List list = null;
try {
sf = HibernateUtil.configureSessionFactoryAnnotation();
session = sf.getCurrentSession();
transaction = session.beginTransaction();
for(int i = 0;i < MOVIE.length;i++) {
query = session.createQuery("from DO_Movie where movieName = :mname and movieLaenge = :mlaenge and fsk = :ffsk");
query.setParameter("mname", MOVIE[i].getMovieName());
query.setParameter("mlaenge", MOVIE[i].getMovieLaenge());
query.setParameter("ffsk", MOVIE[i].getFsk());
list = query.list();
if(list.size() > 0) {
MOVIE[i] = (DO_Movie)list.get(0);
} else {
session.persist(MOVIE[i]);
}
}
transaction.commit();
} catch(Exception ex) {
ex.printStackTrace();
try {
transaction.rollback();
} catch(Exception ex1) {
ex1.printStackTrace();
}
}
return MOVIE;
}
Code:
public void updateMovies(DO_Movie MOVIE) {
SessionFactory sf = null;
Session session = null;
Transaction transaction = null;
try {
sf = HibernateUtil.configureSessionFactoryAnnotation();
session = sf.getCurrentSession();
transaction = session.beginTransaction();
session.update(MOVIE);
transaction.commit();
} catch(Exception ex) {
ex.printStackTrace();
try {
transaction.rollback();
} catch(Exception ex1) {
ex1.printStackTrace();
}
}
}
Code:
public void addNewDVDToExistingMovie(DO_Movie MOVIE, DO_DVD DVD) {
SessionFactory sf = null;
Session session = null;
Transaction transaction = null;
try {
sf = HibernateUtil.configureSessionFactoryAnnotation();
session = sf.getCurrentSession();
transaction = session.beginTransaction();
session.merge(MOVIE);
MOVIE.getDvds().add(DVD);
transaction.commit();
} catch(Exception ex) {
ex.printStackTrace();
try {
transaction.rollback();
} catch(Exception ex1) {
ex1.printStackTrace();
}
}
}
Falls ihr mir helfen könnt oder eine elegantere funktionierende Lösung habt, dann sagt bitte bescheid.
Gruß nathaniell