-->
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.  [ 6 posts ] 
Author Message
 Post subject: Mapping MySQL TEXT column to java string
PostPosted: Sat Sep 24, 2005 4:13 pm 
Newbie

Joined: Sat Sep 24, 2005 3:58 pm
Posts: 6
When retrieving persisted string property mapped to MySQL TEXT type, I'm getting string, containing something like this: "[B@f0a3e8" (address after @ differs on each invoke)

Hibernate 3.0.5
MySQL 4.1.14

Mapping documents:
Code:
<hibernate-mapping package="portix.model">
    <class name="News" table="news">
        <id name="id" type="long" unsaved-value="null">
            <column name="news_id" not-null="true"/>
            <generator class="native"/>
        </id>
        <property name="date" type="timestamp" column="date"/>
        <property name="dayname" type="string" column="dayname" not-null="true" length="16"/>
        <property name="title" type="text" column="title" not-null="true" length="256"/>
        <property name="lead" type="text" column="lead" not-null="true"/>
    </class>
</hibernate-mapping>


`dayname` string contains right value, `title` and `lead` - garbage mentioned before.

News.java:
Code:
public class News {

    private Long mId;

    private Date mDate;
    private String mDayname, mTitle, mLead, mIntro, mText;

    public Long getId() {
        return mId;
    }

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

    public Date getDate() {
        return mDate;
    }

    public void setDate(Date mDate) {
        this.mDate = mDate;
    }

    public String getDayname() {
        return mDayname;
    }

    public void setDayname(String mDayname) {
        this.mDayname = mDayname;
    }

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String mTitle) {
        this.mTitle = mTitle;
    }

    public String getLead() {
        return mLead;
    }

    public void setLead(String mLead) {
        this.mLead = mLead;
    }
}


>.<
Thanks for any help.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 24, 2005 4:27 pm 
Newbie

Joined: Sat Sep 24, 2005 3:58 pm
Posts: 6
Forgot to mention - setting these `text` properties and persisting them to db works just fine. Correct strings are persisted.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 25, 2005 4:33 am 
Newbie

Joined: Sat Sep 24, 2005 3:58 pm
Posts: 6
Addition.
I've simplified everything as I could.
Txt.hbm.xml
Code:
<hibernate-mapping package="portix.model">
    <class name="Txt" table="txt">
        <id name="id" type="long" unsaved-value="null">
            <column name="newstheme_id" not-null="true"/>
            <generator class="native"/>
        </id>
        <property name="txt" type="text" column="txt" not-null="true" />
    </class>
</hibernate-mapping>

Txt.java
Code:
public class Txt {
    private Long id;
    private String txt;

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

    public String getTxt() {
        return txt;
    }
    public void setTxt(String txt) {
        this.txt = txt;
    }
}


Now, in my controller I have following code
Code:
        ModelAndView mav = new ModelAndView("text");
        TxtDAO tdao = HibernateUtil.getDAOFactory().getTxtDAO();

        HibernateUtil.beginTransaction();
        Txt t = new Txt();
        t.setTxt("wowowowow");
        tdao.save(t);
        HibernateUtil.commitTransaction();

        HibernateUtil.beginTransaction();
        mav.addObject("txts", tdao.findAll());
        HibernateUtil.commitTransaction();

        return mav;


Now.
Interesting thing, is that on each invoke, all properties of Txt objects, persisted in previous times, contain garbage like mentioned before, but the one persisted this time contains right value. Apparently, it's because of Hibernate 2nd level caching.
So, it looks like a bug is only in fetching TEXT field from MySQL.
And it's not mine error in mapping java pojo to db, but it's bug in Hibernate. Any suggestions?

Just in case, a bit of involved code more...
TxtDAO.java
Code:
public class TxtDAO extends GenericHibernateDAO<Txt, Long> {

    public TxtDAO(Session session) {
        super(Txt.class, session);
    }
}

GenericHibernateDAO.java
Code:
public class GenericHibernateDAO<T, ID extends Serializable> {

    private Class<T> persistentClass;

    // Session handling
    private Session session;

    protected Session getSession() {
        return session;
    }

    public GenericHibernateDAO(Class<T> persistentClass, Session session) {
        this.persistentClass = persistentClass;
        this.session = session;
    }

    public Class<T> getPersistentClass() {
        return persistentClass;
    }

    public T find(ID id) {
        return find(id, false);
    }

    public T find(ID id, boolean lock) {
        T entity;
        if (lock) {
            entity = (T) getSession().load(getPersistentClass(), id, LockMode.UPGRADE);
        } else {
            entity = (T) getSession().load(getPersistentClass(), id);
        }
        return entity;
    }

    @SuppressWarnings("unchecked")
    public List<T> findAll() {
        return getSession().createCriteria(getPersistentClass()).list();
    }
}


_________________
Another pointless day, where I accomplish nothing (c) Bender@Futurama


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 25, 2005 4:36 am 
Newbie

Joined: Sat Sep 24, 2005 3:58 pm
Posts: 6
Yes, database structure was created using SchemaUpdate
Code:
CREATE TABLE `txt` (
  `newstheme_id` bigint(20) NOT NULL auto_increment,
  `txt` text collate cp1251_bin NOT NULL,
  PRIMARY KEY  (`newstheme_id`)
) ENGINE=MyISAM DEFAULT CHARSET=cp1251 COLLATE=cp1251_bin;

_________________
Another pointless day, where I accomplish nothing (c) Bender@Futurama


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 25, 2005 8:07 am 
Newbie

Joined: Sat Sep 24, 2005 3:58 pm
Posts: 6
Well. Problem solved.

For those, who are interested.

After tracing whole Hibernate code, I've found out, that MySQL J Connector returns to Hibernate this broken value. =)

I've used 3.1.10 version. In 3.2.0-alpha it's fixed, and everything works just fine. :phew:

But cause I don't like the idea of using alpha version in production environment, I'll try looking at versions older than 3.1.10 to see, if whole 3.1.x branch has such problem. I'll post my results later.

_________________
Another pointless day, where I accomplish nothing (c) Bender@Futurama


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 25, 2005 8:19 am 
Newbie

Joined: Sat Sep 24, 2005 3:58 pm
Posts: 6
Yep. Whole 3.1 branch has it.

_________________
Another pointless day, where I accomplish nothing (c) Bender@Futurama


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