2014-09-08 3 views
-1

Je suis nouveau à Hibernate et j'essaie de comprendre le concept oneToMany. J'ai créé deux entités qui sont Personne et Livre. Une personne peut emprunter de nombreux livres et un livre ne peut être emprunté qu'à une seule personne. J'ai donc fait les classes suivantesImpossible de créer une relation oneToMany dans Hibernate

Person.java

import java.util.Date; 
import java.util.HashSet; 
import java.util.Set; 

import javax.persistence.CascadeType; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.OneToMany; 
import javax.persistence.Table; 
import javax.persistence.Temporal; 
import javax.persistence.TemporalType; 

import org.hibernate.annotations.GenericGenerator; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 



@Entity 
@Table(name="PERSON") 
public class Person { 
    private static final Logger _logger = LoggerFactory.getLogger(Person.class); 

    @Id 
    @GenericGenerator(name="adder", strategy="increment") 
    @GeneratedValue(generator="adder") 
    @Column(name="PID") 
    private Long id; 

    @Column(name="FIRSTNAME") 
    private String firstName; 

    @Column(name="LASTNAME") 
    private String lastName; 

    @Column(name="BIRTHDATE") 
    @Temporal(TemporalType.DATE) 
    private Date birthDate; 
    //add genre and books 

    @OneToMany(orphanRemoval=true, mappedBy="person", cascade=CascadeType.ALL,targetEntity=Book.class) 
    private Set<Book> listOfBooks = new HashSet<Book>(); 


    public Set<Book> getListOfBooks() { 
     return listOfBooks; 
    } 

    public void setListOfBooks(Set<Book> listOfBooks) { 
     this.listOfBooks = listOfBooks; 
    } 

    public Long getId() { 
     return id; 
    } 

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


    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 


    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public Date getBirthDate() { 
     return birthDate; 
    } 

    public void setBirthDate(Date birthDate) { 
     this.birthDate = birthDate; 
    } 

    //add constructor with all the details 



} 

Book.java

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 
import javax.persistence.Table; 

import org.hibernate.annotations.GenericGenerator; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

@Entity 
@Table(name="BOOK") 
public class Book { 
    private static final Logger logger = LoggerFactory.getLogger(Book.class); 

    @Id 
    @GenericGenerator(name="adder", strategy="increment") 
    @GeneratedValue(generator="adder") 
    private Long bookId; 

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

    @Column(name="AUTHOR") 
    private String author; 

    @Column(name="PUBLICATION_YEAR") 
    private int yearOfPublication; 

    @Column(name="PUBLISHER") 
    private String publisher; 

    @ManyToOne() 
    @JoinColumn(name="PERSON_ID") 
    private Person person; 


    public Person getPerson() { 
     return person; 
    } 

    public void setPerson(Person person) { 
     this.person = person; 
    } 

    public Long getBookId() { 
     return bookId; 
    } 

    public void setBookId(Long bookId) { 
     this.bookId = bookId; 
    } 


    public String getName() { 
     return name; 
    } 

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

    public String getAuthor() { 
     return author; 
    } 

    public void setAuthor(String author) { 
     this.author = author; 
    } 

    public int getYearOfPublication() { 
     return yearOfPublication; 
    } 

    public void setYearOfPublication(int yearOfPublication) { 
     this.yearOfPublication = yearOfPublication; 
    } 

    public String getPublisher() { 
     return publisher; 
    } 

    public void setPublisher(String publisher) { 
     this.publisher = publisher; 
    } 



} 

HibernateLibraryDaoMain.java

import java.util.Date; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.service.ServiceRegistry; 

import se325.project.assignment1.hibernate.domain.Book; 
import se325.project.assignment1.hibernate.domain.Person; 

    public class HibernateLibraryDaoMain { 

     public static void main(String[] args) { 

      Person person = new Person(); 
      person.setFirstName("Bob"); 
      person.setLastName("Marley"); 
      person.setBirthDate(new Date()); 

      Book book = new Book(); 
      book.setName("Bob Marley book"); 
      book.setAuthor("Lily Marley"); 
      book.setPublisher("Marley Publications"); 
      book.setYearOfPublication(2000); 

      Book book1 = new Book(); 
      book1.setName("Laura Marley book"); 
      book1.setAuthor("Laura Marley"); 
      book1.setPublisher("Laura Publications"); 
      book1.setYearOfPublication(2005); 

      person.getListOfBooks().add(book); 
      person.getListOfBooks().add(book1); 
      book.setPerson(person); 
      book1.setPerson(person); 



      Configuration configuration = new Configuration(); 
      configuration.configure("se325/project/assignment1/hibernate/hibernate.cfg.xml"); 
      ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() 
      .applySettings(configuration.getProperties()).build(); 
      SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); 

      Session session = sessionFactory.openSession(); 
      session.beginTransaction(); 
      session.save(person); 
      session.save(book); 
      session.save(book1); 


      session.getTransaction().commit(); 
      session.close(); 
     } 

    } 

Le problème est que Hibernate ne crée pas Table séparée qui illustre la relation oneToMany. Je n'arrive pas à trouver le problème. Toute aide serait appréciée.

+1

Qu'entendez-vous par table séparée? un à plusieurs n'a pas besoin d'une troisième table au cas où vous vous référez à la même chose. – zerocool

+0

Vous pouvez avoir un ID de personne dans la table book pour créer cette contrainte. – Tony

+0

@zerocool pourriez-vous élaborer plus avant? – Bob

Répondre

0

Je comprends où je me suis trompé. Lorsque j'ai un ensemble de propriétés mappedBy, alors Hibernate ne crée pas une troisième table mais à la place il ajoute à l'entité qui a la propriété ManyToOne et joinColumn. Merci à tous les utilisateurs qui ont aidé :)

+0

cool .. amusez-vous :) – zerocool

+0

vous devez utiliser la table de jointure au lieu de la colonne de jointure –

Questions connexes