Using HOM

23 views
Skip to first unread message

atig

unread,
Oct 19, 2011, 10:20:26 AM10/19/11
to hector-users
Hello new to using HOM / Hector and Cassandra. I have a few very basic
questions below. Would it be possible to use HOM for the following
Pojos.

Class House {
int id;
List<Room> rooms;
}

Class Room {
int id;
String name;
List<Furniture> furniture;
}

Class Furniture {
int id;
String material;
}

Class Table extends Furniture {
String shape;
}

1. Is something like above posisble? Or is that a bad idea?
2. How deep can the nesting of objects be?
3. Is there a document which explains the mapping of different
datatypes and collection?
for eg: Room[] rooms; How would you use HOM on such an array.
4. What is the best practice when using HOM? Design the cassandra
schema first or design the schema based on your Pojo relationships.

Patricio Echagüe

unread,
Oct 19, 2011, 2:04:17 PM10/19/11
to hector...@googlegroups.com
did you check out HOM docs in http://hector-client.org ?

atig

unread,
Oct 19, 2011, 7:20:27 PM10/19/11
to hector-users
Great! Thanks for the link, I have been looking at the wiki on Git
hub. These docs are more explanatory.
It does answer some of the questions for me. I do have a few more.

1. How do you use HOM for compound objects?

Class House {
Room room;
Address address;
}

Class Room {
...
}

Class Address {
...
}

Will this be treated as custom property and thus I would use a custom
converter?

2. How are Object Arrays mapped?

Class House {
Room[] rooms;
}

Thanks for your reply.

On Oct 19, 7:04 pm, Patricio Echagüe <patric...@gmail.com> wrote:
> did you check out HOM docs inhttp://hector-client.org?
>

atig

unread,
Oct 20, 2011, 7:59:36 AM10/20/11
to hector-users
Hi

Had a look at the documentation and tired to implement a list but I
get the following exception when I try to read
the data back.

Exception in thread "main" java.lang.RuntimeException: unsupported
property type, java.util.List
at
me.prettyprint.hom.HectorObjectMapper.determineSerializer(HectorObjectMapper.java:
605)
at
me.prettyprint.hom.converters.DefaultConverter.convertCassTypeToObjType(DefaultConverter.java:
18)
at
me.prettyprint.hom.HectorObjectMapper.setPropertyUsingColumn(HectorObjectMapper.java:
574)
at
me.prettyprint.hom.HectorObjectMapper.createObject(HectorObjectMapper.java:
250)
at
me.prettyprint.hom.HectorObjectMapper.getObject(HectorObjectMapper.java:
112)
at me.prettyprint.hom.EntityManagerImpl.load(EntityManagerImpl.java:
125)
at com.mytest.data.dao.impl.DataModelTest.main(DataModelTest.java:54)
Here's my Pojos

@Entity
@Table(name="Room")
public class Room {
@Id
private UUID id;
@Column(name="drawerList")
private List<Drawer> drawers = new ArrayList<Drawer>();

public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public List<Drawer> getPositions() {
return drawers ;
}
public void setPositions(List<Drawer> drawers) {
this.drawers = drawers;
}
}

public class Drawer implements Serializable {
private static final long serialVersionUID = 8839964032423812617L;

public int hashCode() {
...
}

public boolean equals(Object obj) {
...
}
}

What am I missing here?

Also just wanted to pointed out a little typo in the docs where you
describe the POJO Desk in the documentation.
The getter/setter is using List<String> instead of List<Drawer>

atig

unread,
Oct 20, 2011, 8:08:46 AM10/20/11
to hector-users
Just wanted to add the above reply as there is no way to edit; I have
tried both versions of HOM 1.1.x and 2.0.x with same error.
I thought it might also help for me to paste my keyspace description.

Keyspace: DataTest:
Replication Strategy:
org.apache.cassandra.locator.NetworkTopologyStrategy
Durable Writes: true
Options: [datacenter1:1]
Column Families:
ColumnFamily: Room
Key Validation Class: org.apache.cassandra.db.marshal.BytesType
Default column value validator:
org.apache.cassandra.db.marshal.BytesType
Columns sorted by: org.apache.cassandra.db.marshal.BytesType
Row cache size / save period in seconds: 0.0/0
Key cache size / save period in seconds: 200000.0/14400
Memtable thresholds: 0.2953125/1440/63 (millions of ops/minutes/
MB)
GC grace seconds: 864000
Compaction min/max thresholds: 4/32
Read repair chance: 1.0
Replicate on write: true
Built indexes: []

So basically it's a default keyspace and column family (as of now)
just for testing purposes.
Hopefully someone can point out what I am missing in my
implementation.

atig

unread,
Oct 20, 2011, 10:32:23 AM10/20/11
to hector-users
Hi again,

So I did a little debugging and this is what I find interesting.

The DefaultConverter is able to get the appropriate class type
(ArrayList) when converting Object to byte array in the method
convertObjTypeToCassType
But when I do a load using the EntityManager, the
convertCassTypeToObjType uses class type as (java.util.List) which
causes that exception
as HectorObjectMapper.determineSerializer cannot determine the
serializer correctly.

So I am pretty sure I missing some small property somewhere. If
someone can point that out that would be great.

B. Todd Burruss

unread,
Oct 20, 2011, 2:16:22 PM10/20/11
to hector...@googlegroups.com
regarding this question, you must implement a custom converter,
http://rantav.github.com/hector/build/html/content/HOM/hector-object-mapper.html#custom-property-converters

HOM can only convert basic java types "out of the box", so when you
use a new type, like Room and Address in your example, you need to
provide a converter.

the exeption is when a custom type is used in a java Collection. HOM
uses java serialization.

B. Todd Burruss

unread,
Oct 20, 2011, 2:31:23 PM10/20/11
to hector...@googlegroups.com
(my previous reply was about compound types if it wasn't clear)

i'm looking at the other issues with persisting a list.

do the HOM unit tests work for you?

B. Todd Burruss

unread,
Oct 20, 2011, 2:39:29 PM10/20/11
to hector...@googlegroups.com
The @Column annotation you have on the drawerList property ... is it
@me.prettyprint.hom.annotations.Column or is it
javax.persistence.Column? i bet it is javax.persistence one. if so,
switch to the me.prettyprint.hom.annotations one and it will work

please let me know, because i've been wanting a reason to remove the
javax.persistence references ;)

B. Todd Burruss

unread,
Oct 20, 2011, 2:48:15 PM10/20/11
to hector...@googlegroups.com
... let me correct myself. i forgot that with the introduction of
Collection support, any class marked with java.io.Serializable can be
persisted. it can be more efficient to use your own converter, but by
supporting Serializable any object can be persisted.

On Thu, Oct 20, 2011 at 11:16 AM, B. Todd Burruss <bto...@gmail.com> wrote:

atig

unread,
Oct 20, 2011, 5:15:16 PM10/20/11
to hector-users
@Column is javax.persistence.Column
I was just following the documentation on hector-client.org in which
I assumed that are using javax.persistence.Column
There is no problem in persisting as the correct type is inferred by
the HectorObjectMapper.determineSerializer
only when I load back the data it isn't able to determine the type.
So in my code if I use
@javax.persistence.Column
private List<Drawer> drawers = new ArrayList<Drawer>();
It's persisted properly but while read the above exception is thrown.

If I change my code from
@javax.persistence.Column
private List<Drawer> drawers = new ArrayList<Drawer>();
to
@javax.persistence.Column
private ArrayList<Drawer> drawers = new ArrayList<Drawer>();
save and load work fine.

The documentation does say that Collection<E> persistence is checked
for List and Sets
So I am wondering if it's a bug, because the way it's currently
implemented there is check to get all the interfaces that List
implements (which is Collection<E> and Iterable <E> ) and compare it
to Serializable and hence the exception
Whereas if I use ArrayList<E> ( which implements Serializable,
Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess) and
compared to Serializable there is no exception.

So I am a bit confused.

On Oct 20, 7:48 pm, "B. Todd Burruss" <bto...@gmail.com> wrote:
> ... let me correct myself.  i forgot that with the introduction of
> Collection support, any class marked with java.io.Serializable can be
> persisted.  it can be more efficient to use your own converter, but by
> supporting Serializable any object can be persisted.
>
> On Thu, Oct 20, 2011 at 11:16 AM, B. Todd Burruss <bto...@gmail.com> wrote:
>
>
>
>
>
>
>
> > regarding this question, you must implement a custom converter,
> >http://rantav.github.com/hector/build/html/content/HOM/hector-object-...
>
> > HOM can only convert basic java types "out of the box", so when you
> > use a new type, like Room and Address in your example, you need to
> > provide a converter.
>
> > the exeption is when a custom type is used in a java Collection.  HOM
> > uses java serialization.
>

B. Todd Burruss

unread,
Oct 20, 2011, 5:35:58 PM10/20/11
to hector...@googlegroups.com
if you don't annotate with HOM's @Column, it will not call
ColumnParser.processColumnCustomAnnotation, and therefore not note the
property's type of Collection.

i believe what is happening in your case is that since you are using
javax.persistence.Column, when saving, HOM is discovering that the
ArrayList is Serializable and persisting the object in a single
column. However, when loading, HOM cannot handle the "List" type
because it isn't Serializable. This is a bug, but you still should
use HOM's @Column and things will work properly.

I will make a note of the Serializable bug, thx!

atig

unread,
Oct 21, 2011, 7:05:50 AM10/21/11
to hector-users
Hmm cool that did work and yes, I forgot to mention, the arraylist
wasn't being saved in different columns.
Changing the annotation fixes it.

Thanks for reporting the bug, feel free to link to this thread if you
like.

atig

unread,
Oct 24, 2011, 1:46:35 PM10/24/11
to hector-users
Just wondering how to specfiy a custom converter for Drawer in my
example above
So if I have something like

HOM's @Column
List<Drawer> drawerList;

I would like to specify a custom converter for Drawer. How do I do
that?

B. Todd Burruss

unread,
Oct 24, 2011, 4:36:00 PM10/24/11
to hector...@googlegroups.com
can't do that at the moment. don't have an ETA at the moment. what
you can do with the latest push of code (must compile from master) is
specify a converter for the entire collection.

atig

unread,
Oct 25, 2011, 6:38:34 AM10/25/11
to hector-users
I see. I know you don't have an ETA, but is there a plan to add
support for this?

atig

unread,
Nov 29, 2011, 7:27:05 PM11/29/11
to hector-users
1. Do you plan to add custom converter for Collection element?
2. Can you point me to an example of such converter for entire
collection
3. Can you point me to the code itself that handles a converter for
entire collection?

Thanks

On Oct 25, 10:38 am, atig <anuj.hoo...@gmail.com> wrote:
> I see. I know you don't have an ETA, but is there a plan to add
> support for this?
>
> On Oct 24, 9:36 pm, "B. Todd Burruss" <bto...@gmail.com> wrote:
>
> > can't do that at the moment.  don't have an ETA at the moment.  what
> > you can do with the latest push of code (must compile from master) is
> > specify a converter for the entire collection.
>
> > On Mon, Oct 24, 2011 at 10:46 AM, atig <anuj.hoo...@gmail.com> wrote:
> > > Just wondering how to specfiy a custom converter for Drawer in my
> > > example above
> > > So if I have something like
>
> > >HOM's@Column
> > > List<Drawer> drawerList;
>
> > > I would like to specify a custom converter for Drawer. How do I do
> > > that?
>
> > > On Oct 21, 12:05 pm, atig <anuj.hoo...@gmail.com> wrote:
> > >> Hmm cool that did work and yes, I forgot to mention, the arraylist
> > >> wasn't being saved in different columns.
> > >> Changing the annotation fixes it.
>
> > >> Thanks for reporting the bug, feel free to link to this thread if you
> > >> like.
>
> > >> On Oct 20, 10:35 pm, "B. Todd Burruss" <bto...@gmail.com> wrote:
>

> > >> > if you don't annotate withHOM's@Column, it will not call


> > >> > ColumnParser.processColumnCustomAnnotation, and therefore not note the
> > >> > property's type of Collection.
>
> > >> > i believe what is happening in your case is that since you are using

> > >> > javax.persistence.Column, when saving,HOMis discovering that the


> > >> > ArrayList is Serializable and persisting the object in a single

> > >> > column.  However, when loading,HOMcannot handle the "List" type


> > >> > because it isn't Serializable.  This is a bug, but you still should

> > >> > useHOM's@Column and things will work properly.

> > >> > >> >HOMcan only convert basic java types "out of the box", so when you


> > >> > >> > use a new type, like Room and Address in your example, you need to
> > >> > >> > provide a converter.
>
> > >> > >> > the exeption is when a custom type is used in a java Collection.  HOM
> > >> > >> > uses java serialization.
>
> > >> > >> > On Wed, Oct 19, 2011 at 4:20 PM, atig <anuj.hoo...@gmail.com> wrote:
> > >> > >> >> Great! Thanks for the link, I have been looking at the wiki on Git
> > >> > >> >> hub. These docs are more explanatory.
> > >> > >> >> It does answer some of the questions for me. I do have a few more.
>

> > >> > >> >> 1. How do you useHOMfor compound objects?


>
> > >> > >> >> Class House {
> > >> > >> >> Room room;
> > >> > >> >> Address address;
> > >> > >> >> }
>
> > >> > >> >> Class Room {
> > >> > >> >> ...
> > >> > >> >> }
>
> > >> > >> >> Class Address {
> > >> > >> >> ...
> > >> > >> >> }
>
> > >> > >> >> Will this be treated as custom property and thus I would use a custom
> > >> > >> >> converter?
>
> > >> > >> >> 2. How are Object Arrays mapped?
>
> > >> > >> >> Class House {
> > >> > >> >> Room[] rooms;
> > >> > >> >> }
>
> > >> > >> >> Thanks for your reply.
>
> > >> > >> >> On Oct 19, 7:04 pm, Patricio Echagüe <patric...@gmail.com> wrote:

> > >> > >> >>> did you check outHOMdocs inhttp://hector-client.org?


>
> > >> > >> >>> On Wed, Oct 19, 2011 at 7:20 AM, atig <anuj.hoo...@gmail.com> wrote:

> > >> > >> >>> > Hello new to usingHOM/ Hector and Cassandra. I have a few very basic
> > >> > >> >>> > questions below. Would it be possible to useHOMfor the following


> > >> > >> >>> > Pojos.
>
> > >> > >> >>> > Class House {
> > >> > >> >>> > int id;
> > >> > >> >>> > List<Room> rooms;
> > >> > >> >>> > }
>
> > >> > >> >>> > Class Room {
> > >> > >> >>> > int id;
> > >> > >> >>> > String name;
> > >> > >> >>> > List<Furniture> furniture;
> > >> > >> >>> > }
>
> > >> > >> >>> > Class Furniture {
> > >> > >> >>> > int id;
> > >> > >> >>> > String material;
> > >> > >> >>> > }
>
> > >> > >> >>> > Class Table extends Furniture {
> > >> > >> >>> > String shape;
> > >> > >> >>> > }
>
> > >> > >> >>> > 1. Is something like above posisble? Or is that a bad idea?
> > >> > >> >>> > 2. How deep can the nesting of objects be?
> > >> > >> >>> > 3. Is there a document which explains the mapping of different
> > >> > >> >>> > datatypes and collection?

> > >> > >> >>> > for eg: Room[] rooms; How would you useHOMon such an array.
> > >> > >> >>> > 4. What is the best practice when usingHOM? Design the cassandra

Reply all
Reply to author
Forward
0 new messages