jo-say-yan

Entries tagged as ‘Hibernate’

Hibernate 3 + MySQL 5 + XDoclet2 + HBM2DDL

February 14, 2007 · Leave a Comment

It all started with this error message :

JDBCExceptionReporter - SQL Error: 0, SQLState: 01004
[14 Feb 2007 00:26] ERROR - JDBCExceptionReporter - Data truncation: Out of range value adjusted for column 'USER_STREAMING' at row 1

further investigation found that only “Boolean” property was having this problem. I was using MySQL 5, and hbm2ddl was mapping a “Boolean” property I had on my POJO to a “Bit”, before (on MySQL4) the hbm2ddl Ant task was mapping “Boolean” into “TinyInt(1)”

Changing the column type from “Bit” to “TinyInt(1)” manually on MySQL,  made the error go away. However, I still wanted to avoid manually changing my generated ddl script… I really like xdoclet/hbm2ddl combination (annotations are even better… but later..) and according to this you cn use the “sql-type” to specify the sql type that hbm2ddl can pick up.

So in all, I just had to change my xdoclet tags to:

/**
* @hibernate.property
* @hibernate.column name="USER_
STREAMING" sql-type="BOOLEAN"
*/

That’s all…

Categories: Development
Tagged: ,

Hibernate Annotations – Bidirectional One-To-Many

September 9, 2006 · 31 Comments

I was trying to use Hibernate Annotations, which I think is great … I am happy with it… but I learnt some stuff which I know I will forget. So I am blogging it here, hopefully I will make sense and serve as a reminder for me when I need it. Maybe it might help someone else too… who knows…

Bidirectional One-To-Many
here I am trying to create a Bidirectional One-To-Many relationship between Question and Choice, a Question has many choices.

  1. Setup entities

    @Entity
    @Table (name = "QUESTION")
    public class Question implements Serializable
    {

    @Id
    @GeneratedValue (strategy = GenerationType.AUTO)
    @Column (name = “QUESTION_ID”)
    private Long id;

    @Column (name = “TEXT”)
    private String text;
    ……………


    @Entity
    @Table (name = "CHOICE")
    public class Choice implements Serializable
    {

    @Id
    @GeneratedValue (strategy = GenerationType.AUTO)
    @Column (name = “CHOICE_ID”)
    private Long id;

    @Column (name = “TEXT”)
    private String text;
    ……………

  2. Setup the ONE side on Question – I read ONE Question has MANY CHOICE(S).
    Question

    @OneToMany (mappedBy="question")
    private Set choices = new HashSet();

    mappedBy – means “I am not the owner side”, I am mapped by question from the other side of the relationship. It will also not create the database column which makes sense, I would expect a foreign key on the CHOICE table instead.

  3. Setup the Many side on Choice – I read on Choice.java MANY Choice has ONE Question
    @ManyToOne
    @JoinColumn (name="QUESTION_ID")
    private Question question;

    the @JoinColumn indicate the owning side of the relationship, it is responsible for updating the database column. It will create the QUESTION_ID column on the CHOICE table

  4. Reversing the Relationship so that the owning side is the Question instead
    Question

    @OneToMany
    @JoinColumn (name = "QUESTION_ID")
    private Set choices = new HashSet();

    Choice

    @ManyToOne
    @JoinColumn (name="QUESTION_ID", updatable = false, insertable = false)
    private Question question;

  5. Finally I need to use a List instead of Set – so I can preserve the order of the Choice(s).Question

    @OneToMany (
    cascade = {CascadeType.ALL},
    fetch = FetchType.EAGER
    )
    @JoinColumn (name = "QUESTION_ID")
    @org.hibernate.annotations.Cascade(
    value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN
    )
    @org.hibernate.annotations.IndexColumn(name = "CHOICE_POSITION")
    private List choices = new ArrayList();

    Choice

    @ManyToOne
    @JoinColumn (name="QUESTION_ID", nullable = false, updatable = false, insertable = false)
    private Question question;

    the @org.hibernate.annotations.IndexColumn defines the colum CHOICE_POSITION that will be used to maintain the order of the list. Some how reversing the ownership is the only way to get the IndexColumn to work

Categories: Development
Tagged: ,