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.  [ 3 posts ] 
Author Message
 Post subject: a question ,about one - to - many !!so grotesque
PostPosted: Sat Jul 21, 2007 1:42 am 
Newbie

Joined: Sat Jul 21, 2007 1:31 am
Posts: 2
There are two tables(conferenceinfo,agenda)below:

Code:
CREATE TABLE `conferenceinfo` (
  `conferenceId` bigint(20) NOT NULL auto_increment,

  。。。。
  `version` int(11) NOT NULL default '0',
  PRIMARY KEY  (`conferenceId`),
  UNIQUE KEY `conferenceId` (`conferenceId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


Code:
CREATE TABLE `agenda` (
  `agendaid` bigint(20) NOT NULL auto_increment,
  `conferenceId` bigint(20) NOT NULL,

  。。。.

  `version` int(11) NOT NULL default '0',
  PRIMARY KEY  (`agendaid`),
  UNIQUE KEY `Agendaid` (`agendaid`),
  KEY `conferenceId` (`conferenceId`),
  CONSTRAINT `agenda_ibfk_1` FOREIGN KEY (`conferenceId`) REFERENCES `conferenceinfo` (`conferenceId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;



Conferenceinfo.hbm.xml File:
Code:
<hibernate-mapping>
    <class name="com.avonaco.pojos.Conferenceinfo"
       table="conferenceinfo"
       catalog="cnc_conference"
       dynamic-update="true"
       dynamic-insert="true" >
        <id name="conferenceId" type="java.lang.Long">
            <column name="conferenceId" />
            <generator class="native" />
        </id>
        <version name="version" column="version" />

。。。.....

        <set name="agendas" inverse="true" cascade="all">
            <key>
                <column name="conferenceId" not-null="true" />
            </key>
            <one-to-many class="com.avonaco.pojos.Agenda" />
        </set>

    </class>
</hibernate-mapping>


Agenda.hbm.xml File:
Code:
<hibernate-mapping>
    <class name="com.avonaco.pojos.Agenda"
       table="agenda"
       catalog="cnc_conference"
       dynamic-update="false"
       dynamic-insert="false" >
        <id name="agendaid" type="java.lang.Long">
            <column name="agendaid" />
            <generator class="native" />
        </id>
        <version name="version" column="version" />

        <many-to-one name="conferenceinfo" class="com.avonaco.pojos.Conferenceinfo" fetch="select"
        cascade="none" outer-join="auto" update="true" insert="true" access="property">
            <column name="conferenceId" not-null="true" />
        </many-to-one>
       
    。。。。

    </class>
</hibernate-mapping>





Test Code:

Code:
Conferenceinfo con = new Conferenceinfo();
//test data
con.setTimeZone("1");
     。。。
Agenda ag1 = new Agenda(con,new Date(),"lqk1",new Date(),"lqk1",new Date());
Agenda ag2 = new Agenda(con,new Date(),"lqk2",new Date(),"lqk2",new Date());
Set<Agenda> ags = new HashSet<Agenda>();
ags.add(ag1);
ags.add(ag2);
con.getAgendas().add(ags);
      
this.conferenceinfoDAO.save(con);


So........
Error is occur: Can't get the agendaid ?Why??Why??Why??Why??Why??Why??Why??Why??Why??

Code:
Couldn't get property agendaid from bean [com.avonaco.pojos.Agenda]. Nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 21, 2007 1:58 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
What you did:

Code:
// add the set of agendas as an Agenda
con.getAgendas().add(ags); // NOT WHAT HIB EXPECTS


What you need to do (one or the other, you decide):

Code:
// replace the set with this one
con.setAgendas(ags);

// add these new ones to the existing ones
con.getAgendas().addAll(ags);


It is highly recommended to make get/set of collections protected (or use field access) and expose only helper methods such as addAgenda() and removeAgenda(), etc.

-Chris

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 22, 2007 9:19 pm 
Newbie

Joined: Sat Jul 21, 2007 1:31 am
Posts: 2
Thanks a lot:)

con.setAgendas(ags); is correct


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