Hibernate i mapowanie złożonych obiektów

0

Cześć! Uczę się Hibernate i potrafię już zapisywać proste obiekty do DB, natomiast mam problem z czymś w rodzaju tego poniżej:

Book.java

package org.librarywebservice.pojo;

import java.io.Serializable;

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

import org.hibernate.annotations.ForeignKey;
@Entity
@Table(name = "books")
public class Book implements Serializable {
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Id
	@Column(name = "id")
	private int id;
	@Column(name = "title")
	private String title;
	
	private Author author;
	
	
	public Book(){}
	
	public int getId() {
		return this.id;
	}
	public void setId(int value){
		this.id = value;
	}
	
	public String getTitle(){
		return this.title;
	}
	
	public void setTitle(String value){
		this.title = value;
	}
	public Author getAuhor() {
		return this.author;
	}
	public void setAuthor(Author author){
		this.author = author;
	}
	
}
 

Author.java

package org.librarywebservice.pojo;
import java.io.Serializable;

import javax.persistence.*;

import org.hibernate.annotations.GeneratorType;
@Entity
@Table(name="authors")
public class Author implements Serializable {
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	int id;
	String fullName;
	
	public Author(){}
	
	public int getId(){
		return this.id;
	}
	public void setId(int value){
		this.id = value;
	}
	public String getFullName(){
		return this.fullName;
	}
	public void setFullName(String value){
		this.fullName = value;
	}
}

Chciałbym wywołać metodę save(book) tak, aby automatycznie dodał się też obiekt Author to bazy (jest on w osobnej tabeli). Jak pooznaczać pola, aby działało to jak należy? Znalazłem trochę tutoriali, ale większość dotyczyła mapowania za pomocą plików .xml, reszty szczerze mówiąc nie zrozumiałem. Mógłby ktoś to trochę przybliżyć?

0

Jeśli książka ma jednego autora to dodaj adnotację @OneToOne nad zmienną Author w klasie Book. Jeśli książka może mieć więcej autorów, to @OneToMany, ale wtedy to musi być kolekcja obiektów Author.

0

To apka "edukacyjna" więc zakładam, że autor jest jeden. :) Udało mi się zrobić tak, że dodaje się przy okazji Autor do bazy danych. Musiałem dodać coś w stylu CascadeType.ALL i JoinColumn. Zresztą, wygląda to tak:

Book.java

package org.librarywebservice.pojo;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.ForeignKey;
import org.hibernate.engine.internal.Cascade;
@Entity
@Table(name = "books")
public class Book implements Serializable {

	@GeneratedValue(strategy=GenerationType.AUTO)
	@Id
	@Column(name = "id")
    private int id;

	@Column(name = "title")
	private String title;

	@OneToOne(cascade=CascadeType.ALL)
	@JoinColumn(name="id")
	private Author author;
	
	
	public Book(){}
	
	public int getId() {
		return this.id;
	}
	public void setId(int value){
		this.id = value;
	}
	
	public String getTitle(){
		return this.title;
	}
	
	public void setTitle(String value){
		this.title = value;
	}
	public Author getAuhor() {
		return this.author;
	}
	public void setAuthor(Author author){
		this.author = author;
	}
	
}
 

Natomiast brakuje mi jeszcze, żeby dodało się Id autora to tabeli Books jako klucz obcy. Ale i tak dzięki, bo naprowadziłeś mnie pośrednio na materiały, które to chyba opisują. :) Jakby ktoś coś chciał dodać na temat "best practices", bądź innych rzeczy w tym temacie, to bardzo proszę. ;)

1 użytkowników online, w tym zalogowanych: 0, gości: 1