Duvida JPA/Hibernate como ler property(username) do xml

3 respostas
P

Olá,

tem algum metodo que consigo ler os property do arquivo persistence.xml como :

hibernate.connection.username
hibernate.connection.password
hibernate.connection.url

se alguém puder me ajudar

abs

File: persistence.xml

<persistence 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" version="1.0">
<persistence-unit name="JPAService" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.url" value="jdbc:hsqldb:data/tutorial"/>
</properties>
</persistence-unit>
</persistence>

3 Respostas

mateusprado

Sim ! No objeto AnnotationConfiguration, do Hibernate.
Atravez do método configute(); voce consegue pegar alguma propriedade pelo seu nome, usando o getProperty();

Por exemplo:

public static void main(String[] args) {
		AnnotationConfiguration cfg = new AnnotationConfiguration();

		String senha = cfg.configure().getProperty(
				"hibernate.connection.password");
		String usuario = cfg.configure().getProperty(
				"hibernate.connection.username");
		String url = cfg.configure().getProperty("hibernate.connection.url");

		System.out.println("Conectado a: " + url);
		System.out.println("Usuario: " + usuario);
		System.out.println("Senha: " + senha);


	}
P

estou usando JPA com Hibernate e uso persistence.xml

e tentei usar o que você me deu erro

Erro =org.hibernate.HibernateException: /hibernate.cfg.xml not found

tem como usar com JPA ???

renzonuccitelli

Use um parser de XML, como o SAX, que já é API padrão do JAVA, ou o JColtrane.

Criado 24 de março de 2009
Ultima resposta 24 de mar. de 2009
Respostas 3
Participantes 3