Hibernate version: 3.1 beta2
Relevant mapping documents:
Resposta.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="br.com.scale.trivia.apres.bo.Resposta" abstract="true">
<composite-id>
<key-property name="IDParticipacao"/>
<key-property name="IDPromocao"/>
<key-property name="IDQuestao"/>
</composite-id>
<union-subclass name="br.com.scale.trivia.apres.bo.RespostaMultEscolha" table="RespostaMultEscolha">
<property name="AlternativaCerta"/>
</union-subclass>
<union-subclass name="br.com.scale.trivia.apres.bo.RespostaDissertativa" table="RespostaDissertativa">
<property name="Texto"/>
</union-subclass>
</class>
</hibernate-mapping>
Obs.: RespostaMultEscolha and RespostaDissertativa do not exist as mapping files but only as business objects (that inherit some fields from Resposta) and as tables. Resposta does not exist as table.
Tables:
CREATE TABLE RespostaDissertativa (
IDParticipacao INT NOT NULL,
IDPromocao VARCHAR(12) NOT NULL,
IDQuestao INT NOT NULL,
Texto VARCHAR(460) NOT NULL,
PRIMARY KEY(IDParticipacao,IDPromocao,IDQuestao)
);
CREATE TABLE RespostaMultEscolha (
IDParticipacao INT NOT NULL,
IDPromocao VARCHAR(12) NOT NULL,
IDQuestao INT NOT NULL,
AlternativaCerta INT NOT NULL,
PRIMARY KEY(IDParticipacao,IDPromocao,IDQuestao)
);
Code between sessionFactory.openSession() and session.close():
From DetalhePromocaoAction:
final PromocaoDAO promodao = new PromocaoDAO(HibernateUtil.init_session());
final ParticipacaoDAO partdao = new ParticipacaoDAO(HibernateUtil.get_session());
...useless code..
while(it.hasNext())
{
final Participacao part;
part = (Participacao)it.next();
try {
Hibernate.initialize(part.getRespostas()); // *THROWS* hibernateexception *** TSHTF right here ***
// *INFO* this is needed in order to force hibernate to initialize
// a collection when lazy fetching is enabled.
}
catch(Exception e) {
e.printStackTrace();
// *DESCRIPTION* fora isso nao ha muito o que fazer...
}
}
HibernateUtil.close_current_session();
Full stack trace of any exception that occurs:
rg.hibernate.exception.SQLGrammarException: could not initialize a collection: [br.com.scale.trivia.apres.bo.Participacao.Respostas#component[IDParticipacao,IDPromocao]{IDParticipacao=397, IDPromocao=FASE2MUSEU}]
org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:70)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
org.hibernate.loader.Loader.loadCollection(Loader.java:1923)
org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:520)
org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1555)
org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:436)
org.hibernate.Hibernate.initialize(Hibernate.java:295)
br.com.scale.trivia.admin.action.DetalhePromocaoAction.execute(DetalhePromocaoAction.java:198)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:118)
com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:52)
br.com.scale.trivia.admin.AdminFilter.doFilter(AdminFilter.java:206)
Name and version of the database you are using:
MySQL 4
Obs.: The problem does not occur in my local test computer (which uses MySQL 4 as well I think). But when we uploaded the application to the client's server, which required us to change the .jar for connecting to the database of mysql-connector-java-5.0.0-beta-bin.jar to some other mystical mysql connector that was already present there for eons, we got this problem. The application was compiled with Java SDK 1.4 . I can run it locally with SDK 1.4 without any problems.
The generated SQL (show_sql=true):
load one-to-many br.com.scale.trivia.apres.bo.Participacao.Respostas */
select
respostas0_.IDParticipacao as IDPartic1_1_,
respostas0_.IDPromocao as IDPromocao1_,
respostas0_.IDQuestao as IDQuestao1_,
respostas0_.IDParticipacao as IDPartic1_4_0_,
respostas0_.IDPromocao as IDPromocao4_0_,
respostas0_.IDQuestao as IDQuestao4_0_,
respostas0_.AlternativaCerta as Alternat1_5_0_,
respostas0_.Texto as Texto6_0_,
respostas0_.clazz_ as clazz_0_ from (
select
IDPromocao,
AlternativaCerta,
IDQuestao,
IDParticipacao,
null as Texto, 1 as clazz_
from
RespostaMultEscolha union select
IDPromocao,
null as AlternativaCerta,
IDQuestao,
IDParticipacao,
Texto,
2 as clazz_
from
RespostaDissertativa
)
respostas0_ where respostas0_.IDParticipacao=? and respostas0_.IDPromocao=?
WARN - SQL Error: 1064, SQLState: 42000
ERROR - Syntax error or access violation, message from server: "You have an error in
your SQL syntax. Check the manual that corresponds to your MySQL server version for
the right syntax to use near 'select IDPromocao, AlternativaCerta, IDQuestao, IDParticipacao,"
WARN - Unhandled Exception thrown: class org.hibernate.exception.SQLGrammarException
What do you guys think?
Any help is appreciated.
|