-->
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.  [ 10 posts ] 
Author Message
 Post subject: No persister for java.lang.String
PostPosted: Wed Feb 25, 2004 4:05 am 
Newbie

Joined: Sun Dec 28, 2003 11:10 am
Posts: 8
I have a parent/child relationship between two tables - locale and localelocale (they're to support a multi-lingual application, and locales themselves must also be mult-lingual).

The foreign key in the second/child table (table=localelocale, column=locale_xid, field=localeXid) maps to the primary key in the first/parent table (table=locale, column=x_id, field=xid). I can happily add a 'locale' object, and it saves perfectly.

But I cannot add a 'localelocale' object, either on its own or via a cascade, because I run up against the same error...

"net.sf.hibernate.MappingException: No persister for: java.lang.String"

I've trawled through the docs, and the forums, and only once has someone mentioned this situation, with no real explanation of what the fix was.

I am still not sure if it is a config or code problem, or if it is another example of Hibernate reporting the wrong error message.

Can anybody help?

Version: Hibernate 2.1.2

File: hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/***</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">*****</property>
<property name="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<!-- Mapping files -->
<mapping resource="hibernate.mapping.xml"/>
</session-factory>
</hibernate-configuration>

File: hibernate.mapping.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping default-cascade="save-update">
<class name="net.xyz.data.Locale" table="locale">
<id name="xid" column="x_id" type="string" length="32" unsaved-value="null">
<generator class="uuid.hex"/>
</id>
<property name="xmod" column="x_mod" type="timestamp" not-null="true" />
<property name="xstatus" column="x_status" type="character" not-null="true" />
<property name="code" column="code" type="string" length="5" not-null="true" />
<set name="locales" inverse="true" lazy="true">
<key column="locale_xid" />
<one-to-many class="net.xyz.data.LocaleLocale" />
</set>
</class>
<class name="net.xyz.data.LocaleLocale" table="locale_locale">
<id name="xid" column="x_id" type="string" length="32" unsaved-value="null">
<generator class="uuid.hex"/>
</id>
<property name="xmod" column="x_mod" type="timestamp" not-null="true" />
<property name="xstatus" column="x_status" type="character" not-null="true" />
<property name="localeRefXid" column="locale_ref_xid" type="string" length="32" />
<property name="name" column="name" type="string" length="32" not-null="false"/>
<many-to-one name="localeXid" column="locale_xid" class="net.xyz.data.Locale" not-null="true" />
</class>
</hibernate-mapping>

Code:

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Locale loc = new Locale("en_gb");
session.save(loc);
session.flush();
loc.add(new LocaleLocale("English/Great Britain"));
session.flush();

Stacktrace:

net.sf.hibernate.MappingException: No persister for: java.lang.String
at net.sf.hibernate.impl.SessionFactoryImpl.getPersister(SessionFactoryImpl.java:344)
at net.sf.hibernate.impl.SessionImpl.getClassPersister(SessionImpl.java:2651)
at net.sf.hibernate.impl.SessionImpl.getPersister(SessionImpl.java:2658)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1348)
at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:871)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:838)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:760)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:720)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1354)
at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482)
at net.sf.hibernate.impl.SessionImpl.preFlushEntities(SessionImpl.java:2634)
at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2209)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2198)
at net.xyz.data.Test.<init>(Test.java:33)
at net.xyz.data.Test.main(Test.java:41)

Database: MySQL 4.0.15-nt


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 8:45 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
show your object java code.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 8:52 am 
Newbie

Joined: Sun Dec 28, 2003 11:10 am
Posts: 8
Here's the object code. Hope it helps...!

package net.xyz.data;

import java.sql.*;
import java.util.*;

public class Locale {

private String xid;
private Timestamp xmod;
private char xstatus;

private String code;
private Set locales;

public Locale() {
this.xstatus = 'Y';
this.xmod = new Timestamp(System.currentTimeMillis());
locales = new HashSet();
}

public Locale(String code) {
this();
this.code = code;
}

public void add(LocaleLocale child) {
locales.add(child);
child.setLocale(this);
}

public String getXid() {
return xid;
}

public void setXid(String xid) {
this.xid = xid;
this.xmod = new Timestamp(System.currentTimeMillis());
}

public Timestamp getXmod() {
return xmod;
}

public void setXmod(Timestamp xmod) {
this.xmod = xmod;
}

public char getXstatus() {
return xstatus;
}

public void setXstatus(char sys_status) {
this.xstatus = sys_status;
this.xmod = new Timestamp(System.currentTimeMillis());
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
this.xmod = new Timestamp(System.currentTimeMillis());
}

public Set getLocales() {
return locales;
}

public void setLocales(Set locales) {
this.locales = locales;
}
}

public class LocaleLocale {

private String xid;
private Timestamp xmod;
private char xstatus;

private String localeXid, localeRefXid, name;

public LocaleLocale() {
this.xstatus = 'Y';
this.xmod = new Timestamp(System.currentTimeMillis());
}

public LocaleLocale(String name) {
this();
this.name = name;
}

public void setLocale(Locale locale) {
localeXid = locale.getXid();
}

public String getXid() {
return xid;
}

public void setXid(String xid) {
this.xid = xid;
this.xmod = new Timestamp(System.currentTimeMillis());
}

public Timestamp getXmod() {
return xmod;
}

public void setXmod(Timestamp xmod) {
this.xmod = xmod;
}

public char getXstatus() {
return xstatus;
}

public void setXstatus(char xstatus) {
this.xstatus = xstatus;
this.xmod = new Timestamp(System.currentTimeMillis());
}

public String getLocaleXid() {
return localeXid;
}

public void setLocaleXid(String localeXid) {
System.out.println("ll: setting localeXid from '"+this.localeXid+"' to '"+localeXid+"'");
this.localeXid = localeXid;
this.xmod = new Timestamp(System.currentTimeMillis());
}

public String getLocaleRefXid() {
return localeRefXid;
}

public void setLocaleRefXid(String localeRefXid) {
this.localeRefXid = localeRefXid;
this.xmod = new Timestamp(System.currentTimeMillis());
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
this.xmod = new Timestamp(System.currentTimeMillis());
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 8:53 am 
Newbie

Joined: Sun Dec 28, 2003 11:10 am
Posts: 8
Also, here is the MySQL create table code:

drop table if exists locale;
create table locale (
x_id char(32) not null,
x_mod timestamp not null,
x_status char(1) not null,

code char(5),

primary key(x_id)
);

drop table if exists locale_locale;
create table locale_locale (
x_id char(32) not null,
x_mod timestamp not null,
x_status char(1) not null,

locale_xid char(32),
locale_ref_xid char(32),

name varchar(32),

primary key(x_id),
foreign key(locale_xid) references locale(x_id)
);


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 9:18 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Where is the getLocale() getter ?
I feel unsafe with your setter taking a locale and storing an id.
Your pb is certainly due to a broken getter/setter.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 9:58 am 
Newbie

Joined: Sun Dec 28, 2003 11:10 am
Posts: 8
There is no 'locale' property. The 'setLocale()' method is purely for the Locale class to set the parent ID in the child, since Hibernate does not appear to do this automatically. I thought it would be a little more elegant, but if I remove the 'setLocale' method from the LocaleLocale class, and rework the 'add()' method in the Locale class, so that it sets the child's parent id (localeXid) directly, as follows:

public void add(LocaleLocale child) {
locales.add(child);
child.setLocaleXid(xid);
//child.setLocale(this);
}

I still get the same error.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 10:18 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you have to inverse="false" in this case.
I would recommand to add a real plain Java bean setLocale and getLocale.

try to remove proeprties 1 by 1 to find the offending one.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 10:58 am 
Newbie

Joined: Sun Dec 28, 2003 11:10 am
Posts: 8
I tried changing inverse to false, still get the error. I tried removing all the properties except the keys (see below), still get the error. Just an uninformed guess, but could it be related to the fact that the primary and foreign keys are strings, not numbers?

I'm specifically using Hibernate because I want to build an application that doesn't depend on an App Server, otherwise I would have just used EJB.

new hibernate.mapping.xml:

<hibernate-mapping default-cascade="save-update">
<class name="net.xyz.data.Locale" table="locale">
<id name="xid" column="x_id" type="string" length="32" unsaved-value="null">
<generator class="uuid.hex"/>
</id>
<set name="locales" inverse="false" lazy="true">
<key column="locale_xid" />
<one-to-many class="net.xyz.data.LocaleLocale" />
</set>
</class>
<class name="net.xyz.data.LocaleLocale" table="locale_locale">
<id name="xid" column="x_id" type="string" length="32" unsaved-value="null">
<generator class="uuid.hex"/>
</id>
<many-to-one name="localeXid" column="locale_xid" class="net.xyz.data.Locale" not-null="true" />
</class>
</hibernate-mapping>

I may have to start from first principles again, with something easier to follow than my real example.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 12:10 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I got it

Code:
<many-to-one name="localeXid" column="locale_xid" class="net.xyz.data.Locale" not-null="true" />

means Hibernate expect to set a net.xyz.data.Locale in setLocaleXid(String)

As I did before you should have a proper setLocale(Locale).

Hibernate manage ids for you, stay in OO approch.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 12:56 pm 
Newbie

Joined: Sun Dec 28, 2003 11:10 am
Posts: 8
Thank you so much! I was very close to scrapping it and doing the whole project in pure EJB. Sometimes it's hard to see the wood for the trees. I really appreciate the help you've given. Thanks.


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