Como configurar uma conexão com o hibernate via xml, onde eu posso ta definindo um local dinamico para o meu banco?
Ja procurei em varios lugares e não axei nada, se alguém tiver algum exemplo de como fazer isso eu agradeço!
http://www.guj.com.br/posts/list/59635.java
espero que seja de ajuda, lembrando que para cada banco de dados vc tem um driver/dialect, mas alguns exemplos já estão no tópico então provavelmente já resolverá
Valeu ja ta tudo funcionando certinho, o problema é que ta definido um local fixo de acesso do meu banco de dados, ou seja se eu for roda em outra maquina vou ter que colocar o banco
exatemente onde ta configurado aqui, e outro problema e se eu for roda em rede onde eu dexo o banco em um servidor como eu vo conseguir compartilhar o mesmo banco
se ta definido um local fixo para meu banco, o que eu queria era uma maneira de mandar como parametro pro XML a URL de conexão, ou algum comado do tipo:
que retorna o local que minha aplicação ta assim eu ja conseguiria fazer com que fique dinamico
meu código gerado pelo netBeans do hibernate.cfg.xml:
[code]<?xml version="1.0" encoding="UTF-8"?>
org.hibernate.dialect.FirebirdDialect org.firebirdsql.jdbc.FBDriver//O Problema é aqui, eu não posso definir um local físico para o banco e sim um que eu possa entar mudando
//de acordo com a necessidade
jdbc:firebirdsql:localhost/3050:D:/Meus Documentos/NetBeansProjects/MIC CENTER/Archives/DB/DBMC.FDB
<property name="hibernate.connection.username">SYSDBA</property>
<property name="hibernate.connection.password">*****</property>
<property name="hibernate.connection.autocommit">true</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="hibernate/entity/Orcamentos.hbm.xml"/>
<mapping resource="hibernate/entity/Equipamentos.hbm.xml"/>
<mapping resource="hibernate/entity/Pessoas.hbm.xml"/>
<mapping resource="hibernate/entity/ContasPagar.hbm.xml"/>
<mapping resource="hibernate/entity/Situacao.hbm.xml"/>
<mapping resource="hibernate/entity/OrcamentosProdutos.hbm.xml"/>
<mapping resource="hibernate/entity/Chamadas.hbm.xml"/>
<mapping resource="hibernate/entity/OrcamentosServicos.hbm.xml"/>
<mapping resource="hibernate/entity/Pjuridicas.hbm.xml"/>
<mapping resource="hibernate/entity/Servicos.hbm.xml"/>
<mapping resource="hibernate/entity/Estados.hbm.xml"/>
<mapping resource="hibernate/entity/OrdensServico.hbm.xml"/>
<mapping resource="hibernate/entity/Bancos.hbm.xml"/>
<mapping resource="hibernate/entity/VendasServicos.hbm.xml"/>
<mapping resource="hibernate/entity/Vendas.hbm.xml"/>
<mapping resource="hibernate/entity/Pfisicas.hbm.xml"/>
<mapping resource="hibernate/entity/Categorias.hbm.xml"/>
<mapping resource="hibernate/entity/OrdensServicoSituacao.hbm.xml"/>
<mapping resource="hibernate/entity/ComprasProdutos.hbm.xml"/>
<mapping resource="hibernate/entity/VendasProdutos.hbm.xml"/>
<mapping resource="hibernate/entity/ConfigFuncionario.hbm.xml"/>
<mapping resource="hibernate/entity/Agenda.hbm.xml"/>
<mapping resource="hibernate/entity/Cidades.hbm.xml"/>
<mapping resource="hibernate/entity/Comentarios.hbm.xml"/>
<mapping resource="hibernate/entity/OrdensServicoProdutos.hbm.xml"/>
<mapping resource="hibernate/entity/Compras.hbm.xml"/>
<mapping resource="hibernate/entity/Funcionarios.hbm.xml"/>
<mapping resource="hibernate/entity/ContasReceber.hbm.xml"/>
<mapping resource="hibernate/entity/Produtos.hbm.xml"/>
<mapping resource="hibernate/entity/OrdensServicoServicos.hbm.xml"/>
<mapping resource="hibernate/entity/Pedidos.hbm.xml"/>
<mapping resource="hibernate/entity/PedidosProdutos.hbm.xml"/>
[/code]
<property name="hibernate.connection.url">jdbc:firebirdsql:localhost/3050:D:/Meus Documentos/NetBeansProjects/MIC CENTER/Archives/DB/DBMC.FDB</property>
neste property vc pode apontar para um ip/url onde o banco esteja configurado
Consegui resolve
A solução era que dava pra mim faze via codigo java tudo o que o XML faz, eu não sabia disso
um dia inteiro atrás de uma solução é só agora 1h da manhã consegui e nem era nada dificil
mas ta massa.
RESOLVIDO
fico assim:
public class HibernateUtil {
private static String caminho = System.getProperty("user.dir").replace("\\", "/")+"/Archives/DB/DBMC.FDB";
private static String url;
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
if (!Character.isDigit(caminho.charAt(0)) && caminho.charAt(1)==':'){
url = "jdbc:firebirdsql:localhost/3050:"+caminho;
frmPrincipal.gravaArq();
}else{
String txt = caminho;
String line = null;
txt = txt.replace("//", "");
txt = txt.substring(0,txt.indexOf("/"));
try {
FileInputStream stream = null;
stream = new FileInputStream(System.getProperty("user.dir").replace("\\", "/")+"/Archives/DB/config.txt");
InputStreamReader streamReader = new InputStreamReader(stream);
BufferedReader reader = new BufferedReader(streamReader);
line = reader.readLine();
} catch (IOException ex) {
Logger.getLogger(HibernateUtil.class.getName()).log(Level.SEVERE, null, ex);
}
url = "jdbc:firebirdsql:"+txt+"/3050:"+line.replace("/", "\\");
}
System.out.println(url);
Configuration cfg = new Configuration()
.setProperty("hibernate.dialect",
"org.hibernate.dialect.FirebirdDialect")
.setProperty("hibernate.connection.driver_class",
"org.firebirdsql.jdbc.FBDriver")
.setProperty("hibernate.connection.url",
url)
.setProperty("hibernate.connection.username", "SYSDBA")
.setProperty("hibernate.connection.password", "masterkey")
.setProperty("hibernate.connection.autocommit", "true")
.setProperty("hibernate.show_sql", "true")
.addResource("hibernate/entity/Agenda.hbm.xml")
.addResource("hibernate/entity/Bancos.hbm.xml")
.addResource("hibernate/entity/Categorias.hbm.xml")
.addResource("hibernate/entity/Chamadas.hbm.xml")
.addResource("hibernate/entity/Cidades.hbm.xml")
.addResource("hibernate/entity/Comentarios.hbm.xml")
.addResource("hibernate/entity/Compras.hbm.xml")
.addResource("hibernate/entity/ComprasProdutos.hbm.xml")
.addResource("hibernate/entity/ConfigFuncionario.hbm.xml")
.addResource("hibernate/entity/ContasPagar.hbm.xml")
.addResource("hibernate/entity/ContasReceber.hbm.xml")
.addResource("hibernate/entity/Equipamentos.hbm.xml")
.addResource("hibernate/entity/Estados.hbm.xml")
.addResource("hibernate/entity/Funcionarios.hbm.xml")
.addResource("hibernate/entity/Orcamentos.hbm.xml")
.addResource("hibernate/entity/OrcamentosProdutos.hbm.xml")
.addResource("hibernate/entity/OrcamentosServicos.hbm.xml")
.addResource("hibernate/entity/OrdensServico.hbm.xml")
.addResource("hibernate/entity/OrdensServicoProdutos.hbm.xml")
.addResource("hibernate/entity/OrdensServicoServicos.hbm.xml")
.addResource("hibernate/entity/OrdensServicoSituacao.hbm.xml")
.addResource("hibernate/entity/Pedidos.hbm.xml")
.addResource("hibernate/entity/PedidosProdutos.hbm.xml")
.addResource("hibernate/entity/Pessoas.hbm.xml")
.addResource("hibernate/entity/Pfisicas.hbm.xml")
.addResource("hibernate/entity/Pjuridicas.hbm.xml")
.addResource("hibernate/entity/Produtos.hbm.xml")
.addResource("hibernate/entity/Servicos.hbm.xml")
.addResource("hibernate/entity/Situacao.hbm.xml")
.addResource("hibernate/entity/Vendas.hbm.xml")
.addResource("hibernate/entity/VendasProdutos.hbm.xml")
.addResource("hibernate/entity/VendasServicos.hbm.xml");
sessionFactory = cfg.buildSessionFactory();
// sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}