JPA - dodawanie kilku książek do jednego autora

0

Witam.

Jak mam dodać kilka książek jednemu autorowi.

Chciał bym aby zapisywały się do tabeli Autorzy w kolumnie ksiazka, ale czy tak w ogóle można ?
Czy może trzeba przerobić lekko bazę i zamiast zapisywać id książki tam wpisać id autora ?

No i najważniejsze:

        Ksiazka ksiazka = new Ksiazka();
        ksiazka.setTytul("Pan Tadeusz");
        ksiazka.setRok("1992");
        tryInsert.persist(ksiazka);
 
        Ksiazka ksiazka1 = new Ksiazka();
        ksiazka1.setTytul("Konrad Wallenrod");
        ksiazka1.setRok("1992");
        tryInsert.persist(ksiazka1);
 
        Autor autor = new Autor();
        autor.setId(1L);
        autor.setImie("Adam");
        autor.setNazwisko("Mickiewicz");
        autor.setKsiazka(ksiazka);
        autor.setKsiazka(ksiazka1);
        tryInsert.persist(autor);

Kod autor.setKsiazka(ksiazka) oraz autor.setKsiazka(ksiazka1) nie bardzo mi się podoba, i nie bardzo wiem jak dodać drugą książkę.

Z góry dziękuję za pomoc, pozdrawiam.

0

jeżeli autor ma kilka książek tzn że ma kolekcję książek np. OneToMany. Musisz mieć set/list w autorze. W takim przypadku author_id jest kolumną w tabli książka np. ManyToOne.

0

Mógł bym prosić o kawałek jakiegoś przykładu ?

0

Na podst hibernate:

http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html#entity-hibspec-singleassoc

http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html#entity-hibspec-collection

przy kolekcji pamiętaj o mappedby, hibernate czasmi tworzy śmieciowe tabele.

 
@Entity()
@Table(name = "place")
public class Places extends AbstractEntity {
	...
	
	@NotNull
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = C_AUTHOR, nullable = false)
	private ZpwUser author;
	
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "place")
	private Set<PlacePhoto> photos;

...
}

-----


@Entity()
@DiscriminatorValue("PlacePhoto")
public class PlacePhoto extends UserImage {
	....
	@NotNull
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = C_PLACE)
	private Places place;
         ....
}
0

Kurczę nie bardzo to rozumiem :(

Proszę o podpowiedz na moim kodzie:

Autor:

package jpa;

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

@Entity
@Table(name="AUTORZY")
public class Autor implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "numer_autora")
    private Long id;
    @Column(name="imie")
    private String imie;
    @Column(name="nazwisko")
    private String nazwisko;
    @JoinColumn(name="ksiazka")
    @ManyToOne
    private Ksiazka ksiazka;    

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (getId() != null ? getId().hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Autor)) {
            return false;
        }
        Autor other = (Autor) object;
        if ((this.getId() == null && other.getId() != null) || (this.getId() != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "jpa.Autor[ id=" + getId() + " ]";
    }

    public Long getId() {
        return id;
    }

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

    public String getImie() {
        return imie;
    }

    public void setImie(String imie) {
        this.imie = imie;
    }

    public String getNazwisko() {
        return nazwisko;
    }

    public void setNazwisko(String nazwisko) {
        this.nazwisko = nazwisko;
    }

    public Ksiazka getKsiazka() {
        return ksiazka;
    }

    public void setKsiazka(Ksiazka ksiazka) {
        this.ksiazka = ksiazka;
    }
    
}

Książka:

package jpa;

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;

@Entity
@Table(name="Ksiazki")
public class Ksiazka implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(name="ksiazka")
    private String tytul;
    private String rok;

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (getId() != null ? getId().hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Ksiazka)) {
            return false;
        }
        Ksiazka other = (Ksiazka) object;
        if ((this.getId() == null && other.getId() != null) || (this.getId() != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "jpa.Ksiazka[ id=" + getId() + " ]";
    }

    public Long getId() {
        return id;
    }

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

    public String getTytul() {
        return tytul;
    }

    public void setTytul(String tytul) {
        this.tytul = tytul;
    }

    public String getRok() {
        return rok;
    }
    
    public void setRok(String rok) {
        this.rok = rok;
    }
}
0

public class Autor implements Serializable {
@OneToMany
private Set<Ksiazka> ksiazki;
}

public class Ksiazka implements Serializable {
@manyToOne
private Author author;
}

oj, to się jeszcze dużo musisz uczyć

0

Dzięki !!
Do zamknięcia.

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