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.javaCode:
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.javaCode:
public class TxtDAO extends GenericHibernateDAO<Txt, Long> {
public TxtDAO(Session session) {
super(Txt.class, session);
}
}
GenericHibernateDAO.javaCode:
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();
}
}