V. O arquivo hibernate.cfg.xml e o ehcache.xml
Aqui coloco minha sugestão de configuração para o Hibernate e o EhCache. Logo abaixo do código, colocarei algumas explicações:
- hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- <property name="hibernate.show_sql">true</property> -->
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</property>
<property name="hibernate.cache.provider_configuration">/ehcache.xml</property>
<property name="hibernate.cache.use_minimal_puts">false</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_structured_entries">true</property>
<property name="hibernate.connection.username">mechoffice</property>
<property name="hibernate.connection.password">mechoffice</property>
<mapping resource="mechoffice/entity/Modelo.hbm.xml"/>
<mapping resource="mechoffice/entity/Cadastro.hbm.xml"/>
<mapping resource="mechoffice/entity/Cidade.hbm.xml"/>
<mapping resource="mechoffice/entity/Permissao.hbm.xml"/>
<mapping resource="mechoffice/entity/Tipops.hbm.xml"/>
<mapping resource="mechoffice/entity/Tipo.hbm.xml"/>
<mapping resource="mechoffice/entity/Itensatend.hbm.xml"/>
<mapping resource="mechoffice/entity/Formapagto.hbm.xml"/>
<mapping resource="mechoffice/entity/Veiculo.hbm.xml"/>
<mapping resource="mechoffice/entity/Marca.hbm.xml"/>
<mapping resource="mechoffice/entity/Estado.hbm.xml"/>
<mapping resource="mechoffice/entity/Estoque.hbm.xml"/>
<mapping resource="mechoffice/entity/Menu.hbm.xml"/>
<mapping resource="mechoffice/entity/Categoria.hbm.xml"/>
<mapping resource="mechoffice/entity/Atendimento.hbm.xml"/>
<mapping resource="mechoffice/entity/Prodserv.hbm.xml"/>
</session-factory>
</hibernate-configuration>
- ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
<cache
name="org.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory="5000"
eternal="true"
overflowToDisk="true"/>
<cache
name="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="5"
eternal="false"
timeToLiveSeconds="120"
overflowToDisk="true"/>
</ehcache>
Os dois arquivos devem ficar dentro da pasta src, mas fora da pasta que é o pacote do seu projeto. Esses arquivos serão chamados pelas classes do Hibernate para configuração.
Observem que no arquivo hibernate.cfg.xml, não existem a linha que informa o nome do servidor e o nome do banco de dados que será conectado. Isso é informado pela configuração hibernate.connection.url, mas eu determino isso no executável que chamará a aplicação. Por exemplo: eu posso criar um arquivo .bat, ou um atalho do Windows, ou um arquivo shell executável no Linux com a seguinte linha:
java -Dhibernate.connection.url=jdbc:mysql://servidor:3306/banco -jar NomeDaSuaAplicacaoJava.jar
Se a aplicação estiver em um computador cliente, e o banco de dados no servidor da rede, então basta definir os parâmetros servidor e banco dessa linha.
Outra detalhe é a linha comentada para hibernate.show_sql. Descomente-a se precisar depurar a query na hora de executar a sua aplicação, para avaliar erros ou velocidade.
Espero que isso possa ajudar.
Abs,