Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate Foreign Key Syntax
    primarykey
    data
    text
    <p>I am trying to learn hibernate. I have a movie table with a foreign key to a genre table. Each movie is assigned to a single genre. Each genre may be assigned to many movies.</p> <p>Here are the table definitions:</p> <pre><code>CREATE TABLE `movie` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(64) NOT NULL, `genre_id` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `fk_genre` (`genre_id`), CONSTRAINT `fk_genre` FOREIGN KEY (`genre_id`) REFERENCES `genre` (`id`) ON UPDATE CASCADE, ) CREATE TABLE IF NOT EXISTS `genre` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(32) DEFAULT NULL, PRIMARY KEY (`id`) ) </code></pre> <p>For code I have</p> <pre><code>@Entity public class Movie implements java.io.Serializable { private static final long serialVersionUID = 1; private Integer id; private String title; private Genre genre; ... @ManyToOne public Genre getGenre() { return genre; } </code></pre> <p>Also</p> <pre><code>@Entity public class Genre implements java.io.Serializable { private static final long serialVersionUID = 1; private Integer id; private String name; @Id @GeneratedValue @Column(name = "id") public Integer getId() { return this.id; } </code></pre> <p>Then the select generated by hibernate looks like</p> <pre><code>select movie0_.id as column1_4_, movie0_.genre_id as genre11_4_, movie0_.title as title4_ from Movie movie0_ </code></pre> <p>And this not right as there's no reference to the genre table. The correct query should have a join with the genre table. More like</p> <pre><code>select movie0_.id as column1_4_, genre.name as genre11_4_, movie0_.title as title4_ from Movie movie0_, Genre genre where movie0_.genre_id = genre.id; </code></pre> <p>I'm a little bit of a loss as to what I'm doing wrong. Should the many to one annotation be in the Genre class instead of the Movie class? Or do you see anything else that I'm doing wrong?</p> <p>Based on the advise below, Movie now has</p> <pre><code>@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(id).append(" "); sb.append(title).append(" "); this.getGenre(); //new sb.append(genre.getName()); return sb.toString(); } @ManyToOne(fetch=FetchType.EAGER) //new public Genre getGenre() { return genre; } </code></pre> <p>And the way I'm loading Movie is through</p> <pre><code>public static void main(String[] args) { SessionFactory sf = HibernateUtil.getSessionFactory(); Session session = sf.openSession(); List&lt;Movie&gt; movies = session.createQuery("from Movie").list(); for (Movie movie : movies) { System.out.println(movie.toString()); } session.close(); } </code></pre> <p>What I'm seeing is that even though I have the eager load and I'm explicitly saying getGenre in toString, no query is generated and I'm just getting a null back.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload