-->
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: Collection Mappings
PostPosted: Sat Feb 04, 2006 4:08 am 
Newbie

Joined: Sun Jan 29, 2006 3:37 am
Posts: 10
Hi,

I am having problems with the collection mapping in hibernate. The documentation does not explain enough for me. They only go through and explain the map and then do a few bits and pieces on the other ones throughout that. So anyways, it has made it difficult for me. I have spent the past 5 hours beating my head against this so I'm back with some questions.

1.) Where can I find a list of the required fields for the mappings so I can use it as reference.
Example:
A list mapping requires a list-index field, etc.

This will be handy when I choose what I want to do.

2.) What is a bag? I assume this is just some sort of generic mapping that does not order things and you can use it in conjunction with any collection implementation you use in your code. IE: You can have a list and then map it to a bag, OR you could have a set and map it to a bag.

3.) This is my specific problem. I have tried many different things so I will not provide a lot of detailed information and ask for an explaination of a simple example:
I have a class, lets say nyClass. In that class I have an ArrayList of strings called myList:
ArrayList<String> myList;

I have the setters and the getters for it.

I want to have TWO tables. I want to have a table for myClass and then I want to have a table for the contents of myList.
Like this:
If myClass had this:
myList = "a", "b", "c"
name = "myClass"

The table for myClass would look like so:
Code:
ID Name
1    myClass


The table for myList would look like:
Code:
myClassID  Name
1                   a
2                   b
3                   c


In my mapping file what do I use in order to save this to my DB?
Here are several things I have tried:
Code:
<list name="myList"
           table="listTable">
          <key column="listID"/>
          <list-index column="sortOrder"/>
          <element column="name" type="string"/>
       </list>


This gives the following error:
Exception in thread "main" org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of events.Event.setMyList


I have also tried this:
Code:
<array name="myList" table="listTable">
          <key column="myClass_id"/>
          <list-index column="listID"/>
          <element column="name" type="string"/>
      </array>


This gives the following error:
Exception in thread "main" java.lang.IllegalArgumentException: Argument is not an array

I believe 'array' is for arraylist and 'primitive-array' is for a 'normal' array: int myint[10];

So, basically, I am not entirely sure what to do and any help with my questions would be great!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 05, 2006 10:28 am 
Newbie

Joined: Mon Jan 17, 2005 1:09 pm
Posts: 19
Here is an working example of an list, if you copy this code, it should work without an problem:


the team class
Code:
package test;

import java.util.List;
import java.util.ArrayList;

public class Team {

    Long id;
    String name;
    List players = new ArrayList();

    public Long getId() {return id;}
    public void setId(Long id){this.id = id;}

    public String getName() {return name;}
    public void setName(String name){this.name = name;}

    public List getPlayers(){return players;}
    public void setPlayers(List players){this.players = players;}

    public void addPlayer(String player){
        this.players.add(player);
    }
}


The Team mapping
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="test.Team" table="teams">

        <id name="id" column="id">
            <generator class="native"/>
        </id>

        <property name="name"/>

        <list name="players" table="players">
            <key column="team_id"/>
            <index column="number"/>
            <element type="string" column="playerName"/>
        </list>

    </class>

</hibernate-mapping>


an test class with an main method
Code:
package test;

import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


public class HibTestMain {
    public static void main(String[] args){
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

        Session session = sessionFactory.openSession();

        Transaction  tx = session.beginTransaction();

        Team t = new Team();
        t.setName("The Simpon Giants");
        t.addPlayer("Homer");
        t.addPlayer("Bart");
        t.addPlayer("Lisa");

        session.save(t);

        tx.commit();

        session.close();
    }
}


My hibernate configuration file
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">paswoord</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <mapping resource="test/Team.hbm.xml"/>
    </session-factory>
</hibernate-configuration>



This is code I tested an it saves everything as it should.

Hopefully this example helps you to get started


I saw following mistakes in your code:
<list name="myList"
table="listTable">
<key column="listID"/>
<list-index column="sortOrder"/>===> list index is only used for Arrays ellements not List elements.
<element column="name" type="string"/>
</list>

An arrayList is an List and not an array ==> so that explains your "Exception in thread "main" java.lang.IllegalArgumentException: Argument is not an array " error. The Array element is only used for primitive arrays


PS: don't forget to rate


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.