Erro de gramática ao inserir dado utilizando Hibernate

0 respostas
rafael.espiritosanto

Pessoal

Em meu programa está ocorrendo a seguinte exceção
21/03/2009 12:44:49 org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 1064, SQLState: 42000
21/03/2009 12:44:49 org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: 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 'fullText, medlineNumber, paperNumber, recordNumber, source, title, titleAbstract' at line 1
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not insert: [br.ufrj.cos.bri.model.DocumentModel]
	at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
	at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:226)
	at br.ufrj.cos.bri.controller.CarregaXMLJDOM.main(CarregaXMLJDOM.java:199)
Caused by: org.hibernate.exception.SQLGrammarException: could not insert: [br.ufrj.cos.bri.model.DocumentModel]
	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
	at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)
	at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2186)
	at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2666)
	at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
	at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
	at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
	at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
	at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
	at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:49)
	at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:154)
	at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:110)
	at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
	at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:645)
	at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:619)
	at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:623)
	at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:220)
	... 1 more
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: 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 'fullText, medlineNumber, paperNumber, recordNumber, source, title, titleAbstract' at line 1
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
	at com.mysql.jdbc.Connection.execSQL(Connection.java:3256)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1313)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1585)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1500)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1485)
	at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
	at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
	... 16 more

Este é o código que faz a inserção dos dados.

package br.ufrj.cos.bri.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.apache.commons.collections.map.HashedMap;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import br.ufrj.cos.bri.model.Author;
import br.ufrj.cos.bri.model.Cite;
import br.ufrj.cos.bri.model.DocumentModel;
import br.ufrj.cos.bri.model.Subtopic;


public class CarregaXMLJDOM {
	public static void main(String[] args) {
		try {
			EntityManagerFactory emf = Persistence.createEntityManagerFactory("CarregaXMLBancoDados");
			EntityManager em = emf.createEntityManager();
			EntityTransaction tx = em.getTransaction();
			tx.begin();
			
			File file = new File("cf74.xml");
			
			SAXBuilder sb = new SAXBuilder(true);
			Document d = sb.build(file);
			Element root = (Element) d.getRootElement();
			List children = root.getChildren();
			
			Iterator it = children.iterator();
			
			List<DocumentModel> listDocuments = new ArrayList<DocumentModel>();
			
			while (it.hasNext()) {
				StringBuilder stringBuilderTitleAbstract = new StringBuilder();
				StringBuilder stringBuilderFullText = new StringBuilder();
				
				Element e = (Element) it.next();
				
				DocumentModel documentModel = new DocumentModel();
				
				System.out.println(e.getName());
				documentModel.setPaperNumber(e.getChildText("PAPERNUM"));
				
				String title = e.getChildText("TITLE");
				documentModel.setTitle(title);
				String conteudoAbstractExtract = e.getChildText("ABSTRACT");
				if (conteudoAbstractExtract != null) {
					documentModel.setAbstractText(conteudoAbstractExtract);
				} else {
					conteudoAbstractExtract = e.getChildText("EXTRACT");
					documentModel.setAbstractText(conteudoAbstractExtract);
				}
				stringBuilderTitleAbstract.append(title + " " + conteudoAbstractExtract);
				documentModel.setTitleAbstract(stringBuilderTitleAbstract.toString());
				
				documentModel.setMedlineNumber(e.getChildText("MEDLINENUM"));
				documentModel.setRecordNumber(e.getChildText("RECORDNUM"));
				
				documentModel.setSource(e.getChildText("SOURCE"));
				
				/*List<Author> listAuthors = new ArrayList<Author>();
				Element elementAuthors = e.getChild("AUTHORS");
				if (elementAuthors != null) {
					List list = elementAuthors.getChildren();
					Iterator iterator = list.iterator();
					
					while (iterator.hasNext()) {
						Element a = (Element) iterator.next();
						String nameAuthor = a.getText();
						Author author = new Author();
						author.setName(nameAuthor);
						listAuthors.add(author);
					}
				}*/
				
				Map<String, Author> autores = new HashedMap();
				Element elementAuthors = e.getChild("AUTHORS");
				if (elementAuthors != null) {
					List list = elementAuthors.getChildren();
					Iterator iterator = list.iterator();
					
					while (iterator.hasNext()) {
						Element a = (Element) iterator.next();
						String nameAuthor = a.getText();
						Author author = new Author();
						author.setName(nameAuthor);
						autores.put(nameAuthor, author);
					}
				}
				//documentModel.setAutores(autores);
				
				
				
				List<Subtopic> listMajorSubj = new ArrayList<Subtopic>();
				Element elementMajorSubj = e.getChild("MAJORSUBJ");
				if (elementMajorSubj != null) {
					List list = elementMajorSubj.getChildren();
					Iterator iterator = list.iterator();
					
					while (iterator.hasNext()) {
						Element a = (Element) iterator.next();
						String majorSubj = a.getText();
						Subtopic topic = new Subtopic();
						topic.setTopic(majorSubj);
						listMajorSubj.add(topic);
						em.persist(topic);
					}
				} 
				documentModel.setMajorSubtopic(listMajorSubj);
				
				List<Subtopic> listMinorSubj = new ArrayList<Subtopic>();
				Element elementMinorSubj = e.getChild("MINORSUBJ");
				if (elementMinorSubj != null) {
					List list = elementMinorSubj.getChildren();
					Iterator iterator = list.iterator();
					
					while (iterator.hasNext()) {
						Element a = (Element) iterator.next();
						String minorSubj = a.getText();
						Subtopic topic = new Subtopic();
						topic.setTopic(minorSubj);
						listMinorSubj.add(topic);
						em.persist(topic);
					}
				} 
				documentModel.setMinorSubtopic(listMinorSubj);
				
				List<Cite> listRefences = new ArrayList<Cite>();
				Element elementReferences = e.getChild("REFERENCES");
				if (elementReferences != null) {
					List list = elementReferences.getChildren();
					Iterator iterator = list.iterator();
					
					while (iterator.hasNext()) {
						Element a = (Element) iterator.next();
						Cite cite = new Cite();
						
						String num = a.getAttribute("num").getValue();
						String author = a.getAttribute("author").getValue();
						String publication = a.getAttribute("publication").getValue();
						String d1 = a.getAttribute("d1").getValue();
						String d2 = a.getAttribute("d2").getValue();
						String d3 = a.getAttribute("d3").getValue();
						
						cite.setAuthor(author);
						cite.setD1(d1);
						cite.setD2(d2);
						cite.setD3(d3);
						cite.setNum(Integer.parseInt(num));
						cite.setPublication(publication);
						
						em.persist(cite);
						listRefences.add(cite);
					}
				}
				documentModel.setReferences(listRefences);
				
				List<Cite> listCitations = new ArrayList<Cite>();
				Element elementCitations = e.getChild("CITATIONS");
				if (elementCitations != null) {
					List list = elementCitations.getChildren();
					Iterator iterator = list.iterator();
					
					while (iterator.hasNext()) {
						Element a = (Element) iterator.next();
						Cite cite = new Cite();
						
						String num = a.getAttribute("num").getValue();
						String author = a.getAttribute("author").getValue();
						String publication = a.getAttribute("publication").getValue();
						String d1 = a.getAttribute("d1").getValue();
						String d2 = a.getAttribute("d2").getValue();
						String d3 = a.getAttribute("d3").getValue();
						
						cite.setAuthor(author);
						cite.setD1(d1);
						cite.setD2(d2);
						cite.setD3(d3);
						cite.setNum(Integer.parseInt(num));
						cite.setPublication(publication);
						
						em.persist(cite);
						listCitations.add(cite);
					}
				}
				documentModel.setCitations(listCitations);
				em.persist(documentModel);
				
				listDocuments.add(documentModel);
			}
			tx.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Ao tentar persistir a classe DocumentModel ocorre a exceção listada acima.
O que pode estar acontecendo?

Obrigado!

Criado 21 de março de 2009
Respostas 0
Participantes 1