Hibernate: simple JOIN

603 views
Skip to first unread message

K.

unread,
Mar 10, 2014, 7:32:12 PM3/10/14
to dropwiz...@googlegroups.com
Hey!

I hope you can help me with my obviously simple problem. :)

I am using the Dropwizard Framework with the integrated hibernate to access a MySQL database and now I would like to do a simple JOIN.

I think it is good to break down the problem to a simple Room<-->Person example:
So, given the following sample table structure with Person(ID,ROOM_ID) and ROOM(ID,NAME):

[SQL]
CREATE TABLE IF NOT EXISTS
`PERSON` (
 
`ID` VARCHAR(100) NOT NULL,
 
`ROOM_ID` INT NULL,
  PRIMARY KEY
(`ID`))
ENGINE
= InnoDB;

CREATE TABLE IF NOT EXISTS
`ROOM` (
 
`ID` INT NOT NULL AUTO_INCREMENT,
 
`NAME` VARCHAR(45) NULL,
  PRIMARY KEY
(`ID`))
ENGINE
= InnoDB;
[/SQL]



... I would like to join PERSON p with ROOM r ON p.ROOM_ID=r.ID. --> NOTHING special at all :(
Of course, pure SQL works:

[SQL]
SELECT p
.ID,r.NAME FROM PERSON p INNER JOIN ROOM r ON p.ROOM_ID=r.ID
[/SQL]



So, since I am using Hibernate, there are two sample POJOs:

[JAVA]
@Entity
@Table(name = "PERSON")
@NamedQuery(name = "...", query = "SELECT p,r FROM Person p INNER JOIN FETCH p.room r")
public class Person
{
   
@Id
   
@Column(name = "ID")
   
private String ID;

   
@ManyToOne(targetEntity = Room.class)
   
@Fetch(FetchMode.JOIN)
   
@JoinColumn(name = "ID")
   
private Room room;
   
   
//getter, setter, stuff :)
}

@/*just to clarify:*/javax.persistence.Entity
@Table(name = "ROOM")
public class Room
{
   
@Id
   
private int ID;

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

   
@OneToMany(targetEntity = Person.class)
   
@JoinColumn(name = "ROOM_ID")
   
private Set<Person> persons;

   
//again ... getter, setter, stuff
}
[/JAVA]


And when I start the Dropwizard project, there occurs the following error:

org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class com.bla.bla.Room.persons[this.is.shit.isn't.it.Person]

So, what are the problems? :)

mve...@gmail.com

unread,
Mar 10, 2014, 7:38:04 PM3/10/14
to dropwiz...@googlegroups.com
This looks like a hibernate specific issue vs a dropwizard issue. 

Matt

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-us...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

K.

unread,
Mar 10, 2014, 7:57:07 PM3/10/14
to dropwiz...@googlegroups.com
Yes, after hours of googling it seems that something is wrong with the hibernate configuration. But since there are no standard hibernate configurations xml-files in Dropwizard, I think it might suit better in this group :) I can't imagine that I am the first Dropwizard user who needs to perform a JOIN withe hibernate ^^

Carlo Barbara

unread,
Mar 10, 2014, 8:47:36 PM3/10/14
to dropwiz...@googlegroups.com
My guess based on this garbled error "org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class com.bla.bla.Room.persons[this.is.shit.isn't.it.Person]", I would say that one of the two entities in the relationship haven't been added to the list of classes used to initialize the hibernate bundle.

K.

unread,
Mar 10, 2014, 9:27:57 PM3/10/14
to dropwiz...@googlegroups.com
Hm, I thought that too :( But it looks good:

[JAVA]
private final HibernateBundle<ApiConfiguration> hibernate_person = new HibernateBundle<ApiConfiguration>(
       
Person.class) {
   
@Override
   
public DataSourceFactory getDataSourceFactory(
       
ApiConfiguration configuration)
   
{
       
return configuration.getDataSourceFactory();
   
}

   
};

private final HibernateBundle<ApiConfiguration> hibernate_room = new HibernateBundle<ApiConfiguration>(
       
Room.class) {
   
@Override
   
public DataSourceFactory getDataSourceFactory(
       
ApiConfiguration configuration)
   
{
       
return configuration.getDataSourceFactory();
   
}

   
};

@Override
   
public void initialize(Bootstrap<ApiConfiguration> bootstrap)
   
{
//...
    bootstrap
.addBundle(hibernate_person);
    bootstrap
.addBundle(hibernate_room);
//...
}
[/JAVA]


Ramakrishna Nalam

unread,
Mar 10, 2014, 10:00:17 PM3/10/14
to dropwiz...@googlegroups.com

Why are there different hibernate bundles for different classes!
Please use the same bundle if you want to do things like joining across them.
The HibernateBundle constructor can take in multiple classes, so just pass in both the classes you have to it. That's one mistake I see.

Different bundles would probably be for use case where you want to connect to different db's.

Regards,
Rama.

K.

unread,
Mar 10, 2014, 10:47:36 PM3/10/14
to dropwiz...@googlegroups.com
Oups, thank you! That was a good suggestion :) I really thought I have to use multiple HibernateBundles.
Reply all
Reply to author
Forward
0 new messages