-->
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.  [ 5 posts ] 
Author Message
 Post subject: Join with annotations explained
PostPosted: Tue Jan 16, 2007 7:42 am 
Newbie

Joined: Tue Jan 16, 2007 7:34 am
Posts: 9
Hi

I'm new to hibernate and I'm using annotations. I've read documentation but it's no clear for me what to do.

I want to do a join and see in docs the following example:

Code:

@Entity
public class Trainer {
    @OneToMany
    @JoinTable(
            name="TrainedMonkeys",
            joinColumns = { @JoinColumn( name="trainer_id") },
            inverseJoinColumns = @JoinColumn( name="monkey_id")
    )
    public Set<Monkey> getTrainedMonkeys() {
    ...
}

@Entity
public class Monkey {
    ... //no bidir
}




I understand the association between both tables but, which code should I place in getTrainedMonkeys ?

Should I call to a Monkey.getMonkeys method passing a trainer_id as parameter ? Does Hibernate this automatically ?

Sorry if it's a so newbie question but I'm not sure what to do or how Hibernate does it.

Thanks in advance

C


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 8:02 am 
Newbie

Joined: Mon Jan 15, 2007 3:22 pm
Posts: 6
The purpose of the annotations is to tell Hibermate what should be in the collection so that it can populate it automatically. As you are trying to do nothing clever other than retrieve the list of monkeys for a given trainer all you need to do is:

Code:
private Set<Monkey> trainedMonkeys;

    @OneToMany
    @JoinTable(
            name="TrainedMonkeys",
            joinColumns = { @JoinColumn( name="trainer_id") },
            inverseJoinColumns = @JoinColumn( name="monkey_id")
    )
public Set<Monkey> getTrainedMonkeys(){
    return this.trainedMonkeys;
}

public void setTrainedMonkeys(Set<Monkey> monkeys){
    this.trainedMonkeys = monkeys;
}


Hibernate will manage the contents of the collection, ensuring that it contains the relevant monkeys. You have to be sure that the Trainer is bound to a Session when the getTrainedMonkeys() method is called though (or use FetchType.EAGER)

Matt


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 8:59 am 
Newbie

Joined: Tue Jan 16, 2007 7:34 am
Posts: 9
Madeye wrote:
The purpose of the annotations is to tell Hibermate what should be in the collection so that it can populate it automatically. As you are trying to do nothing clever other than retrieve the list of monkeys for a given trainer all you need to do is:

Code:
private Set<Monkey> trainedMonkeys;

    @OneToMany
    @JoinTable(
            name="TrainedMonkeys",
            joinColumns = { @JoinColumn( name="trainer_id") },
            inverseJoinColumns = @JoinColumn( name="monkey_id")
    )
public Set<Monkey> getTrainedMonkeys(){
    return this.trainedMonkeys;
}

public void setTrainedMonkeys(Set<Monkey> monkeys){
    this.trainedMonkeys = monkeys;
}


Hibernate will manage the contents of the collection, ensuring that it contains the relevant monkeys. You have to be sure that the Trainer is bound to a Session when the getTrainedMonkeys() method is called though (or use FetchType.EAGER)

Matt



Thank you !!

C


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 12:52 pm 
Newbie

Joined: Tue Jan 16, 2007 7:34 am
Posts: 9
Hi

Following examples I did:

Code:
@Entity
@Table(name = "periodos")
public class Periodos implements Serializable {
    @OneToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name="PeriodosHorarios",
            joinColumns = { @JoinColumn( name="id_per") },
            inverseJoinColumns = @JoinColumn( name="id_horarios")
    )

    /**
     * Id del periodo.
     */
    private int id_per;

   /** Used to join tables */
    private Set<Horarios> periodosHorarios;

    public Set<Horarios> getPeriodosHorarios() {
        return periodosHorarios;
    }

    public void setPeriodosHorarios(Set<Horarios> periodosHorarios) {
        this.periodosHorarios = periodosHorarios;
    }



and

Code:
@Entity
@Table(name = "horarios")
public class Horarios implements Serializable {

    /**
     * PK.
     */
    private int id_horarios;

    /**
     * FK to periodos.
     */
    private int id_per;

   @Column(name = "id_per", nullable = false, unique = false)
    public int getId_per() {
        return id_per;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public int getId_horarios() {
        return id_horarios;
    }




But when I start my application I get the error:

Code:

17:35:27,250 ERROR [ContextLoader] Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean wit
h name 'sessionFactory' defined in ServletContext resource [/WEB-INF/application
Context-dao.xml]: Invocation of init method failed; nested exception is org.hibe
rnate.MappingException: Could not determine type for: java.util.Set, for columns
: [org.hibernate.mapping.Column(periodosHorarios)]
Caused by:
org.hibernate.MappingException: Could not determine type for: java.util.Set, for
columns: [org.hibernate.mapping.Column(periodosHorarios)]
        at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:266)
        at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
        at org.hibernate.mapping.Property.isValid(Property.java:185)
        at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:4
10)
        at org.hibernate.mapping.RootClass.validate(RootClass.java:192)
        at org.hibernate.cfg.Configuration.validate(Configuration.java:1099)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.jav
a:1284)
        at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSession
Factory(LocalSessionFactoryBean.java:804)
        at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessi
onFactory(LocalSessionFactoryBean.java:744)
        at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPr
opertiesSet(AbstractSessionFactoryBean.java:131)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1118)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.initializeBean(AbstractAutowireCapableBeanFactory.java:1085)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.createBean(AbstractAutowireCapableBeanFactory.java:429)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getOb
ject(AbstractBeanFactory.java:250)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistr
y.getSingleton(DefaultSingletonBeanRegistry.java:141)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean
(AbstractBeanFactory.java:247)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean
(AbstractBeanFactory.java:161)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.
preInstantiateSingletons(DefaultListableBeanFactory.java:270)
        at org.springframework.context.support.AbstractApplicationContext.refres
h(AbstractApplicationContext.java:346)
        at org.springframework.web.context.support.AbstractRefreshableWebApplica
tionContext.refresh(AbstractRefreshableWebApplicationContext.java:156)
        at org.springframework.web.context.ContextLoader.createWebApplicationCon
text(ContextLoader.java:246)
        at org.springframework.web.context.ContextLoader.initWebApplicationConte
xt(ContextLoader.java:184)
        at org.springframework.web.context.ContextLoaderListener.contextInitiali
zed(ContextLoaderListener.java:49)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContex
t.java:3763)




Why this error ?? I did what I understood from samples...

Thanks in advance

C


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 4:56 pm 
Newbie

Joined: Tue Jan 16, 2007 7:34 am
Posts: 9
Please, could somebody give me a clue with this ?


Thanks in advance

C


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.