-->
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: Association mapping, column naming and deserialization
PostPosted: Tue Dec 29, 2009 2:56 am 
Newbie

Joined: Tue Sep 25, 2007 6:30 am
Posts: 10
Hi all !
I come to you with a quite annoying problem.
Ok so let's set up the background.
I'm trying to map an association between two objects : a Vehicle an a Component, a vehicle having possibly several components of different types (i show only two in the sample code below).

My Vehicle class is like that :
Code:
@Entity
@Table(name = "VEHICLE")
public class Vehicle extends UUIDBaseEntity {
/** The vehicle color. */
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="COLOR_ID")
    private Component color;
    /** The vehicle brand. */
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="BRAND_ID")
    private Component brand;
    /**
     * @return the color
     */
    @Column(name="COLOR_ID", columnDefinition="VARCHAR(36)")
    public Component getColor() {
        return color;
    }
    /**
     * @param color the color to set
     */
    public void setColor(Component color) {
        this.color = color;
    }
    /**
     * @return the brand
     */
    @Column(name="BRAND_ID", columnDefinition="VARCHAR(36)")
    public Component getBrand() {
        return brand;
    }
    /**
     * @param brand the brand to set
     */
    public void setBrand(Component brand) {
        this.brand = brand;
    }
}


Here is why i put the annotations above.
  • @JoinColumn, because the objects are supposed to be persisted in an existing table, and the vehicle is linked to its components through components ids (here brand_id and color_id).
  • @Column on the getter defining two elements :
    • The column name : because for testing i use H2 database having hibernate creating the schema before the tests, and if i don't add the @Column annotation on the getter, then the column name is called Brand or Color, based on the getter/setter names i guess. But i need it to be brand_id and color_id, to be able to fit existing database and to use direct SQL.
    • The column definition, as a varchar(36) (uuid) because if i don't add this, the column is defined as a varbinary in H2 by hibernate, so direct sql searches on the uuid as a string does not work. And as i expect my persistence in production to be in the existing database with a schema not managed by hibernate, with varchar2(36) uuids, i have to use the same schema for testing.

The component class is straightforward, mapped in its own table, with default column names and no association.

So my problems are :
  • Why hibernate does not use JoinColumn name for column naming (brand_id for example) ?
  • Is it normal to have to use Column on the getter to force this? And is there a conflict between JoinColumn and Column?
  • Why is hibernate using varbinary by default ?
  • Finally, when i try to retrieve vehicles from hibernate, i get an error : org.hibernate.type.SerializationException: could not deserialize, caused by : java.io.StreamCorruptedException: invalid stream header: 7B3D055A. Wether i let hibernate use its varbinary or i force varchar(36), same result, only the number (7B3D055A) changes. Can someone show me what's wrong? I must say i debugged, and hibernate fails on the first component (brand_id).

I would really appreciate any help...
Thanks !


Top
 Profile  
 
 Post subject: Re: Association mapping, column naming and deserialization
PostPosted: Tue Dec 29, 2009 4:21 am 
Newbie

Joined: Tue Sep 25, 2007 6:30 am
Posts: 10
Well i fixed my problems. I placed annotations on property getters. I was wrong because i was extending a class defining the @Id and this annotation was on the getter. So my annotations on the fields were not taken into consideration, as the @Id annotation defines wether hibernate uses field or property access. That's why the @JoinColumn had no effect.

By the way when i placed the JoinColumn annotation on the properties getters, i had plenty of foreign key errors, because my dataset imported for test was not correct in terms of relations. I fixed my data exporter and everything is fine now.

Hopes this solution helps. Check where the @Id is !!! ;)


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