-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate one-to-many mapping (annotations)
PostPosted: Fri Apr 18, 2014 3:11 pm 
Newbie

Joined: Fri Apr 18, 2014 2:55 pm
Posts: 1
Hi,

I am new to hibernate, I am trying to learn it and I have run into a problem trying to make one to many relationship's work. I have tried several examples but none seem to be working.

Can someone have a look at the below code and tell me where I have gone wrong. I have tried many different tutorials but it always doesn't work, so I must be missing something.

There are two classes: MovieDb and Genre. For each movie there will be many Genre's.

@Column(name = "movieId", insertable = true, updatable = true)
If I set this line to false, there are no error messages but movieId on moviesGenre table is blank, everything else inserts correctly.
If I set this line to true, I get the following error: Repeated column in mapping for entity: com.medialibrary.api.model.Genre column: movieId (should be mapped with insert="false" update="false")

I believe I have included all the files and information needed, if not let me know.

Thanks for the help.

MovieDb.java
Code:
@Entity
@Table(name = "movies")
public class MovieDb extends IdElement {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @JsonProperty("id")
    @Column(name = "movieId")
    private int movieId;

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

    @JsonProperty("genres")
    @OneToMany(mappedBy="movie", cascade = CascadeType.ALL)
    private List<Genre> genres;

    public int getMovieId() { return movieId; }
    public String getTitle() { return title; }
    public List<Genre> getGenres() { return genres; }

    public void setMovieId(int movieId) { this.movieId = movieId; }
    public void setTitle(String title) { this.title = title; }
    public void setGenres(List<Genre> genres) { this.genres = genres; }
}

Genre.java
Code:
@JsonRootName("genre")
@Entity
@Table(name = "moviesGenre")
public class Genre implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id")
    private int id;

    @Column(name = "movieId", insertable = true, updatable = true)
    private int movieId;

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

    @ManyToOne
    @JoinColumn(name="movieId")
    private MovieDb movie;

    public int getMovieId() { return movieId; }
    public String getName() { return name; }
    public MovieDb getMovie() { return movie; }

    public void setMovieId(int movieId) { this.movieId = movieId; }
    public void setName(String name) { this.name = name; }
    public void setMovie(MovieDb movie) { this.movie = movie; }
}

Main.java
Code:
public MovieDb getMovie(int movieId) {
    Session s = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = s.beginTransaction();
    MovieDb movie = null;
    try {
        String hql = "FROM MovieDb E WHERE E.movieId = " + movieId;
        Query query = s.createQuery(hql);
        List movies = query.list();
        movie = (MovieDb)movies.get(0);
        tx.commit();
    }catch (HibernateException ex) {
        if (tx != null)
            tx.rollback();
    }finally {
        s.close();
    }
    return movie;
}

public void saveMovie(MovieDb movie) {
    Session s = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = s.beginTransaction();
    try {
        s.save(movie);
        s.flush();
        tx.commit();
    } catch (HibernateException ex) {
        if (tx != null)
            tx.rollback();
    } finally {
        s.close();
    }
}

My database is setup like this:
movies
Code:
id             int           (primary)
movieId     int
title          varchar


moviesGenre
Code:
id             int           (primary)
movieId     int
name        varchar

My Hibernate.cfg.xml file showing mapping is below:
Hibernate.cfg.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    ...
    ...
    ...
    <mapping class="com.medialibrary.api.model.MovieDb"/>
    <mapping class="com.medialibrary.api.model.Genre"/>
  </session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject: Re: Hibernate one-to-many mapping (annotations)
PostPosted: Fri Apr 18, 2014 3:41 pm 
Newbie

Joined: Mon Apr 07, 2014 10:20 am
Posts: 12
You have this mapping on your Genre entity:

Code:
    @Column(name = "movieId", insertable = true, updatable = true)
    private int movieId;
    ...
    @ManyToOne
    @JoinColumn(name="movieId")
    private MovieDb movie;


That error is because you have the movieId column mapped twice. Hibernate doesn't know which mapping actually "owns" this value (in the situation where they might not agree on your entity, which value should actually persist?).

I think you can simply change your insertable = true, updatable = true to "false" instead. But, in this situation you may not really need it mapped twice. You can change your getter to look like this instead:

Code:
    public int getMovieId() { return (movie != null) ? movie.getMovieId() : null; }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.