Galera sou novo com Hibernate e não estou conseguindo fazer o codigo rodar de jeito nenhum, não estou conseguindo fazer a entidade ser enxergada!
Metodo MAIN
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
/**
*
*/
public class TesteCidade {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpatestesPU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Cidade cidade = new Cidade();
cidade.setDescricao("Rio Verde");
em.persist(cidade);
em.getTransaction().commit();
}
}
Classe CIDADE
package testehibernate;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author RSB
*/
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name = "cidade")
@SequenceGenerator(name="seq_cidade", sequenceName="cidade_cod_cidade_seq")
public class Cidade implements Serializable {
private static final long serialVersionUID = 1L;
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="seq_cidade")
private long cod_cidade;
private String descricao;
public long getCod_cidade() {
return cod_cidade;
}
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
public void setCod_cidade(long cod_cidade) {
this.cod_cidade = cod_cidade;
}
}
A minha persistencia.xml é essa:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="jpatestesPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.username" value="projeto**"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.password" value="****"/>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/hibernate"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="hibernate.hbm2ddl.auto" value="none"/>
</properties>
</persistence-unit>
</persistence>
Alguem tem alguma dica doque tenho de fazer?
vlw galera!
Ola, abaixo do fechamento da tag properties, vc tem que mapear as classes. meuPacote.MinhaEntidade ex:
-
<?xml version="1.0" encoding="UTF-8"?>
-
-
- org.hibernate.ejb.HibernatePersistence
-
-
-
-
-
-
-
-
- meuPacote.MinhaEntidade
-
-
Olá Leandro, fiz o mapeamento da classe como pediu:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="jpatestesPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.username" value="projetonewbusca"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.password" value="newbusca"/>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/hibernate"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="hibernate.hbm2ddl.auto" value="none"/>
</properties>
<class>testehibernate.Cidade</class>
</persistence-unit>
</persistence>
Só que agora o erro mudou para:
INFO: Hibernate EntityManager 3.3.2.GA
25/08/2009 16:44:19 org.hibernate.ejb.packaging.PersistenceXmlLoader$ErrorLogger error
SEVERE: Error parsing XML: XML InputStream(13) cvc-complex-type.2.4.d: Invalid content was found starting with element 'class'. No child element is expected at this point.
Exception in thread "main" javax.persistence.PersistenceException: invalid persistence.xml
at org.hibernate.ejb.packaging.PersistenceXmlLoader.loadURL(PersistenceXmlLoader.java:72)
at org.hibernate.ejb.packaging.PersistenceXmlLoader.deploy(PersistenceXmlLoader.java:89)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:222)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:125)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
at testehibernate.TesteCidade.main(TesteCidade.java:24)
Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.d: Invalid content was found starting with element 'class'. No child element is expected at this point.
Se Poder continuar a me ajudar seria de grande ajuda! vlw ai!
manda seu arquivo persistence.xml novamente com a alteração
oks está aqui:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="jpatestesPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.username" value="projetonewbusca"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.password" value="newbusca"/>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/hibernate"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="hibernate.hbm2ddl.auto" value="none"/>
</properties>
<class>testehibernate.Cidade</class>
</persistence-unit>
</persistence>
vlw
remova testehibernate.Cidade
e insira embaixo da tag provider
[quote=leandrolw]remova testehibernate.Cidade
e insira embaixo da tag provider[/quote]
Olá amigo, fiz isso e parou de dar esse erro ai!
Mas, como eu não sei mapear a sequence que criei ele continua dando erro
SEVERE: ERROR: relation "seq_cidade" does not exist
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not get next sequence value
mas acho que esse não deve ser tão dificil assim… vou ver se acho algo na net! vlw
[quote=rodrigoajato][quote=leandrolw]remova testehibernate.Cidade
e insira embaixo da tag provider[/quote]
Olá amigo, fiz isso e parou de dar esse erro ai!
Mas, como eu não sei mapear a sequence que criei ele continua dando erro
SEVERE: ERROR: relation "seq_cidade" does not exist
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not get next sequence value
mas acho que esse não deve ser tão dificil assim… vou ver se acho algo na net! vlw
[/quote]
Falto criar a Sequence…
SchemaExport se = new SchemaExport(cfg);
se.create(true, true);
vlw
poxa em uma apliação JAVA SE voce tem que lista no xml todas as classes entity obrigatoriamente? ele não varre a aplicação e mapeia as classes anotadas com @Entity? não tem como configurar para ele fazer isto.
Não precisa não amigão… Por isso um colega acima pediu para REMOVER isso:
<class>testehibernate.Cidade</class>
abraço