Guevara 13 de set. de 2010
Olá!
Crie uma pasta META-INF dentro da pasta /src do seu projeto, lá dentro da META-INF vc cria seu persistence.xml, e dentro desse arquivo vc coloca a configuração:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version= "2.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_2_0.xsd" >
<persistence-unit name= "default" transaction-type= "RESOURCE_LOCAL" >
<provider> org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name= "hibernate.dialect" value= "org.hibernate.dialect.PostgreSQLDialect" />
<property name= "javax.persistence.jdbc.url" value= "jdbc:postgresql://localhost/meubanco" />
<property name= "javax.persistence.jdbc.driver" value= "org.postgresql.Driver" />
<property name= "javax.persistence.jdbc.password" value= "123456" />
<property name= "javax.persistence.jdbc.user" value= "postgres" />
<property name= "hibernate.hbm2ddl.auto" value= "update" />
<property name= "hibernate.show_sql" value= "true" />
<property name= "hibernate.format_sql" value= "true" />
</properties>
</persistence-unit>
</persistence>
Substitua o usuario, senha e banco de acordo com os seus dados. Eu uso Hibernate e JPA, ai vc coloca de acordo com o que vc está usando, mas o esqueleto do arquivo é esse.
Abraço!
Metaleiro 13 de set. de 2010
[color=darkblue] Muito obrigado, estava na pasta errada, porém quando criei a pasta gerou o seguinte erro :[/color]
Exception in thread "main" java . lang . UnsupportedOperationException : The user must supply a JDBC connection
at org . hibernate . connection . UserSuppliedConnectionProvider . getConnection ( UserSuppliedConnectionProvider . java : 30 )
at org . hibernate . jdbc . ConnectionManager . openConnection ( ConnectionManager . java : 423 )
at org . hibernate . jdbc . ConnectionManager . getConnection ( ConnectionManager . java : 144 )
at org . hibernate . jdbc . AbstractBatcher . prepareQueryStatement ( AbstractBatcher . java : 139 )
at org . hibernate . loader . Loader . prepareQueryStatement ( Loader . java : 1547 )
at org . hibernate . loader . Loader . doQuery ( Loader . java : 673 )
at org . hibernate . loader . Loader . doQueryAndInitializeNonLazyCollections ( Loader . java : 236 )
at org . hibernate . loader . Loader . doList ( Loader . java : 2220 )
at org . hibernate . loader . Loader . listIgnoreQueryCache ( Loader . java : 2104 )
at org . hibernate . loader . Loader . list ( Loader . java : 2099 )
at org . hibernate . loader . hql . QueryLoader . list ( QueryLoader . java : 378 )
at org . hibernate . hql . ast . QueryTranslatorImpl . list ( QueryTranslatorImpl . java : 338 )
at org . hibernate . engine . query . HQLQueryPlan . performList ( HQLQueryPlan . java : 172 )
at org . hibernate . impl . SessionImpl . list ( SessionImpl . java : 1121 )
at org . hibernate . impl . QueryImpl . list ( QueryImpl . java : 79 )
at org . hibernate . ejb . QueryImpl . getResultList ( QueryImpl . java : 66 )
at br . com . digicon . psecertdb . dao . CertificadoDao . listAllCriteria ( CertificadoDao . java : 34 )
at br . com . digicon . testes . luciano . TestaCertificadoCriteria . main ( TestaCertificadoCriteria . java : 25 )
[color=darkblue] Muito obrigado pela ajuda ![/color]
Guevara 13 de set. de 2010
Vc está usando Hibernate + JPA? Se não estiver vc precisa adaptar o persistence.xml de acordo com o que estiver usando. Existem vários exemplos para pegar como referência.
Vc esta usando EJB3?
Ejb3Configuration.configure:210]
Vai ter q adaptar para isso ai. Como eu ainda não usei o EJB3 não vou saber te orientar na configuração, mas vc acha isso pesquisando no fórum ou no google.
Metaleiro 13 de set. de 2010
[color=darkblue]Obrigado pela ajuda, foi o que você falou ! [/color]
cassia.sf 28 de dez. de 2013
Guevara:
Olá!
Crie uma pasta META-INF dentro da pasta /src do seu projeto, lá dentro da META-INF vc cria seu persistence.xml, e dentro desse arquivo vc coloca a configuração:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version= "2.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_2_0.xsd" >
<persistence-unit name= "default" transaction-type= "RESOURCE_LOCAL" >
<provider> org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name= "hibernate.dialect" value= "org.hibernate.dialect.PostgreSQLDialect" />
<property name= "javax.persistence.jdbc.url" value= "jdbc:postgresql://localhost/meubanco" />
<property name= "javax.persistence.jdbc.driver" value= "org.postgresql.Driver" />
<property name= "javax.persistence.jdbc.password" value= "123456" />
<property name= "javax.persistence.jdbc.user" value= "postgres" />
<property name= "hibernate.hbm2ddl.auto" value= "update" />
<property name= "hibernate.show_sql" value= "true" />
<property name= "hibernate.format_sql" value= "true" />
</properties>
</persistence-unit>
</persistence>
Substitua o usuario, senha e banco de acordo com os seus dados. Eu uso Hibernate e JPA, ai vc coloca de acordo com o que vc está usando, mas o esqueleto do arquivo é esse.
Abraço!
Não estava colocando no classpath do projeto, agora corrigi o erro.
Porém apareceu outro erro no console:
Caused by: javax.persistence.PersistenceException: Invalid persistence.xml.
Error parsing XML [line : -1, column : -1] : cvc-elt.1: Não pode localizar a declaração do elemento ‘persistence’.
Uso JPA 2.1,Hibernate 4.3 e Postegresql 9.3
Meu persistence.xml está assim:
<persistence version= "2.1" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xmlns= "http://java.sun.com/xml/ns/persistence"
xsi:schemalocation= "http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd" >
<persistence-unit name= "default" transaction-type= "RESOURCE_LOCAL" >
<provider > org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name= "hibernate.dialect" value= "org.hibernate.dialect.PostgreSQLDialect" ></property>
<property name= "javax.persistence.jdbc.url" value= "jdbc:postgresql://localhost:5432/Automoveis" ></property>
<property name= "javax.persistence.jdbc.user" value= "postgres" ></property>
<property name= "javax.persistence.jdbc.password" value= "1234" ></property>
<property name= "javax.persistence.jdbc.driver" value= "org.postgresql.jdbc.Driver" ></property>
<property name= "hibernate.show_sql" value = "true" />
<property name= "hibernate.format_sql" value = "true" />
<property name= "hibernate.hbm2ddl.auto" value= "create" ></property>
</properties>
</persistence-unit>
</persistence>
Metaleiro 29 de dez. de 2013
Segue um exemplo funcional do persistence.xml
& lt ;? xml version = "1.0" encoding = "UTF-8" ? & gt ;
& lt ; persistence version = "2.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_2_0.xsd" & gt ;
& lt ; persistence - unit name = "JSFCrudPU" transaction - type = "RESOURCE_LOCAL" & gt ;
& lt ; provider & gt ; org . hibernate . ejb . HibernatePersistence & lt ; / provider & gt ;
& lt ; properties & gt ;
& lt ; property name = "javax.persistence.jdbc.driver" value = "org.postgresql.Driver" /& gt ;
& lt ; property name = "javax.persistence.jdbc.url" value = "jdbc:postgresql://localhost:5432/JSFCrudDB" /& gt ;
& lt ; property name = "javax.persistence.jdbc.user" value = "postgres" /& gt ;
& lt ; property name = "javax.persistence.jdbc.password" value = "postgres" /& gt ;
& lt ; property name = "hibernate.dialect" value = "org.hibernate.dialect.PostgreSQLDialect" /& gt ;
& lt ; property name = "hibernate.connection.shutdown" value = "true" /& gt ;
& lt ; property name = "hibernate.hbm2ddl.auto" value = "update" /& gt ;
& lt ; property name = "hibernate.show_sql" value = "false" /& gt ;
& lt ; property name = "hibernate.format_sql" value = "false" /& gt ;
& lt ; / properties & gt ;
& lt ; / persistence - unit & gt ;
& lt ; / persistence & gt ;
cassia.sf 29 de dez. de 2013
Carmba nada funciona :o
Quando coloco tag :
<?xml version="1.0" encoding="UTF-8"?>
Da erro :
O destino da instrução de processamento correspondente "[xX][mM][lL]" não é permitido.
Se tiro a tag roda e da o erro:
Não pode localizar a declaração do elemento 'persistence'.
Minha versão da JPA é 2.1, então no lugar de 2.0 coloco 2.1 ?????????
É a primeira vez que utilizo e acredito estar deixando alguma coisa passar...
Meu persistense.xml ficou assim:
& lt ;? xml version = & quot ; 1.0 & quot ; encoding = & quot ; UTF - 8 & quot ;? & gt ;
& lt ; persistence version = "2.1"
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_2_1.xsd" & gt ;
& lt ; persistence - unit name = "JSFCrudPU" transaction - type = "RESOURCE_LOCAL" & gt ;
& lt ; provider & gt ; org . hibernate . ejb . HibernatePersistence & lt ; / provider & gt ;
& lt ; properties & gt ;
& lt ; property name = "javax.persistence.jdbc.driver" value = "org.postgresql.Driver" /& gt ;
& lt ; property name = "javax.persistence.jdbc.url" value = "jdbc:postgresql://localhost:5432/Automoveis" /& gt ;
& lt ; property name = "javax.persistence.jdbc.user" value = "postgres" /& gt ;
& lt ; property name = "javax.persistence.jdbc.password" value = "1234" /& gt ;
& lt ; property name = "hibernate.dialect" value = "org.hibernate.dialect.PostgreSQLDialect" /& gt ;
& lt ; property name = "hibernate.hbm2ddl.auto" value = "update" /& gt ;
& lt ; property name = "hibernate.show_sql" value = "true" /& gt ;
& lt ; property name = "hibernate.format_sql" value = "true" /& gt ;
& lt ; / properties & gt ;
& lt ; / persistence - unit & gt ;
& lt ; / persistence & gt ;
Metaleiro:
Segue um exemplo funcional do persistence.xml
& lt ;? xml version = & quot ; 1.0 & quot ; encoding = & quot ; UTF - 8 & quot ;? & gt ;
& lt ; persistence version = "2.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_2_0.xsd" & gt ;
& lt ; persistence - unit name = "JSFCrudPU" transaction - type = "RESOURCE_LOCAL" & gt ;
& lt ; provider & gt ; org . hibernate . ejb . HibernatePersistence & lt ; / provider & gt ;
& lt ; properties & gt ;
& lt ; property name = "javax.persistence.jdbc.driver" value = "org.postgresql.Driver" /& gt ;
& lt ; property name = "javax.persistence.jdbc.url" value = "jdbc:postgresql://localhost:5432/JSFCrudDB" /& gt ;
& lt ; property name = "javax.persistence.jdbc.user" value = "postgres" /& gt ;
& lt ; property name = "javax.persistence.jdbc.password" value = "postgres" /& gt ;
& lt ; property name = "hibernate.dialect" value = "org.hibernate.dialect.PostgreSQLDialect" /& gt ;
& lt ; property name = "hibernate.connection.shutdown" value = "true" /& gt ;
& lt ; property name = "hibernate.hbm2ddl.auto" value = "update" /& gt ;
& lt ; property name = "hibernate.show_sql" value = "false" /& gt ;
& lt ; property name = "hibernate.format_sql" value = "false" /& gt ;
& lt ; / properties & gt ;
& lt ; / persistence - unit & gt ;
& lt ; / persistence & gt ;
Metaleiro 29 de dez. de 2013
Eu uso exatamente dessa maneira com o :
<?xml version="1.0" encoding="UTF-8"?>
Você tem certeza que está no diretório correto ?
Dentro do META-INF ?
Você deixou o name com o nome do projeto que mandei, tente mudar também:
& lt ; persistence - unit name = "JSFCrudPU" transaction - type = "RESOURCE_LOCAL" & gt ;
& lt ; persistence - unit name = "default" transaction - type = "RESOURCE_LOCAL" & gt ;
cassia.sf 29 de dez. de 2013
Colei aqui antes de editar mas arrumei sim o name...
Tem algo mal no meu xml, mas não consigo perceber...
Exatamente assim como postei abaixo da o erro: The processing instruction must begin with the name of the target.
E no console : A instrução de processamento deve começar com o nome do destino.
<? Xml version = "1.0" encoding = "UTF-8" >
< persistence version = "2.1"
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_2_1.xsd" >
< persistence - unit name = "default" transaction - type = "RESOURCE_LOCAL" >
< provider > org . hibernate . ejb . HibernatePersistence </ provider >
< properties >
< property name = "javax.persistence.jdbc.driver" value = "org.postgresql.Driver" />
< property name = "javax.persistence.jdbc.url" value = "jdbc:postgresql://localhost:5432/Automoveis" />
< property name = "javax.persistence.jdbc.user" value = "postgres" />
< property name = "javax.persistence.jdbc.password" value = "1234" />
< property name = "hibernate.dialect" value = "org.hibernate.dialect.PostgreSQLDialect" />
< property name = "hibernate.hbm2ddl.auto" value = "update" />
< property name = "hibernate.show_sql" value = "true" />
< property name = "hibernate.format_sql" value = "true" />
</ properties >
</ persistence - unit >
</ persistence >
cassia.sf 29 de dez. de 2013
Ahhhhhhhhhhhhh deu certo!
Tirei a tag xml version...
Mas uma dúvida ficou: se a versão da JPA é 2.1 porque só funcionou com 2.0 no persistente.xml?????????????
Obrigada pessoal!!!!
Ficou assim: :D
<persistence version= "2.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_2_0.xsd" >
<persistence-unit name= "default" transaction-type= "RESOURCE_LOCAL" >
<provider> org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name= "javax.persistence.jdbc.driver" value= "org.postgresql.Driver" />
<property name= "javax.persistence.jdbc.url" value= "jdbc:postgresql://localhost:5432/Automoveis" />
<property name= "javax.persistence.jdbc.user" value= "postgres" />
<property name= "javax.persistence.jdbc.password" value= "1234" />
<property name= "hibernate.dialect" value= "org.hibernate.dialect.PostgreSQLDialect" />
<property name= "hibernate.hbm2ddl.auto" value= "update" />
<property name= "hibernate.show_sql" value= "true" />
<property name= "hibernate.format_sql" value= "true" />
</properties>
</persistence-unit>
</persistence>