I have been trying since hours to set up JNDI in my project
My Hibernate file is like this :
<?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.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">mypass</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#big-data-3.logti.etsmtl.ca:1521:LOG660</property>
<property name="hibernate.connection.username">myTeam</property>
<property name="hibernate.default_schema">teamAlpha</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.search.autoregister_listeners">false</property>
<mapping resource="Scenariste.hbm.xml" />
<mapping resource="Utilisateur.hbm.xml" />
<mapping resource="Annonce.hbm.xml" />
<mapping resource="Genre.hbm.xml" />
<mapping resource="Location.hbm.xml" />
<mapping resource="Pays.hbm.xml" />
<mapping resource="Personnelfilm.hbm.xml" />
<mapping resource="Clientenretard.hbm.xml" />
<mapping resource="Film.hbm.xml" />
<mapping resource="Forfait.hbm.xml" />
<mapping resource="Employe.hbm.xml" />
<mapping resource="Client.hbm.xml" />
<mapping resource="Personneltournage.hbm.xml" />
</session-factory>
I'm aware to add JNDI it has to be something similar to this
hibernate.connection.datasource = java:/comp/env/jdbc/test
hibernate.transaction.factory_class = \
org.hibernate.transaction.JTATransactionFactory
hibernate.transaction.manager_lookup_class = \
org.hibernate.transaction.JBossTransactionManagerLookup
hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
But how to fill the datasource and factory class and lookup class based on my info? my database name is teamAlpha
I'm using JBOSS pluggin for eclipse ,
Thank you
Related
I have a problem because I don't know how to connect to SQL server with hibernate file I try this
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database Connection settings -->
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=RentApp</property>
<property name="hibernate.connection.username">LAPTOP-SJJ2BT5V\Konrad</property>
<property name="hibernate.connection.password"></property>
<!-- Echo SQL wyswietlenie zapytania tabeli bazy danych -->
<property name="hibernate.show_sql">true</property>
<!-- SQL Dialect implementacja silnika bazo danego -->
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- database intialize -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- mappings -->
<mapping class="pl.edu.wszib.model.Vehicle" />
<mapping class="pl.edu.wszib.model.User" />
<mapping class="pl.edu.wszib.model.GroupVehicle" />
<mapping class="pl.edu.wszib.model.Rent" />
<mapping class="pl.edu.wszib.model.Buy" />
</session-factory>
</hibernate-configuration>
In SQL server my database name is RentApp and I use windows authentication in the Microsoft SQL Server.
So
<property name="hibernate.connection.username">LAPTOP-SJJ2BT5V\Konrad</property>
<property name="hibernate.connection.password"></property>
username must be from Microsoft SQL Server?
And I use in my pom
<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.2.2.jre11</version>
</dependency>
I have an error right that but I have username: LAPTOP-SJJ2BT5V\Konrad
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'LAPTOP-SJJ2BT5V\Konrad'.
I try this
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=Test;integratedSecurity=true</property>
But I have error:
com.microsoft.sqlserver.jdbc.SQLServerException: This driver is not configured for integrated authentication.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database Connection settings -->
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=Test;integratedSecurity=true</property>
<property name="hibernate.connection.username">LAPTOP-SJJ2BT5V\Konrad</property>
<property name="hibernate.connection.password"></property>
<!-- Echo SQL wyswietlenie zapytania tabeli bazy danych -->
<property name="hibernate.show_sql">true</property>
<!-- SQL Dialect implementacja silnika bazo danego -->
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- database intialize -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- mappings -->
<mapping class="pl.edu.wszib.model.Vehicle" />
<mapping class="pl.edu.wszib.model.User" />
<mapping class="pl.edu.wszib.model.GroupVehicle" />
<mapping class="pl.edu.wszib.model.Rent" />
<mapping class="pl.edu.wszib.model.Buy" />
</session-factory>
</hibernate-configuration>
and i must download sqljdbc_auth.dll and put this file to C:\Program Files\Java\jdk-11.0.1\bin
Is some way to drop and create table on start of hibernate session? But only one from few mapped tables.
My hibernate configuration:
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/smart_home</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">false</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="pl.smart.home.database.User" />
<mapping class="pl.smart.home.database.UnrecognisedDevice" />
<mapping class="pl.smart.home.database.Sensor" />
<mapping class="pl.smart.home.database.TypeDevice" />
<mapping class="pl.smart.home.database.Event" />
<mapping class="pl.smart.home.database.RecognisedDevice" />
<mapping class="pl.smart.home.database.Status" />
</session-factory>
I want drop and create "event" table.
You can use create-drop
<property name="hbm2ddl.auto">create-drop</property>
Drop the schema and recreate it on SessionFactory startup. Additionally, drop the schema on SessionFactory shutdown.
I'm new to java and hibernate framework, can you please explain to me if I have to make a Hibernate cfg file for every table I have or only this is enough. At the moment I have this and it takes care of a person table, now if I want to also work with a new table (exam in my case) do I need to write a new file and change the mapping?
<?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.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3310/scheduler</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">blanked-out-as-this-is-s3cr3t</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.scheduler.backend.model.Person"></mapping>
</session-factory>
</hibernate-configuration>
You just need only ONE cfg file, if you need to add a new table just post it as
<mapping class="com.scheduler.backend.model.NEW_MODEL"></mapping>
So the file be like this:
<?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.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3310/scheduler</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">blanked-out-as-this-is-s3cr3t</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.scheduler.backend.model.Person"></mapping>
<mapping class="com.scheduler.backend.model.NEW_ONE"></mapping>
<mapping class="com.scheduler.backend.model.NEW_ONE"></mapping>
</session-factory>
</hibernate-configuration>
I have JSF Application with Hibernate. I use Tomcat connection pool in hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.PostgreSQL9Dialect</property>
<!-- using Tomcat connections -->
<property name="connection.datasource">java:/comp/env/jdbc/netstat</property>
<!-- Parameters for Hibernate connection provider
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/netstat</property>
<property name="connection.username">netstat</property>
<property name="connection.password">netstat</property>
-->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- configuration pool via c3p0 -->
<!--
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">100</property>
<property name="c3p0.max_statements">200</property>
<property name="c3p0.timeout">600</property>
-->
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping classes -->
<mapping class="ru.gooamoko.model.Group" />
<mapping class="ru.gooamoko.model.Host" />
<mapping class="ru.gooamoko.model.Traffic" />
<mapping class="ru.gooamoko.model.DailyTraffic" />
</session-factory>
</hibernate-configuration>
Project can be build with maven with all tests skipped and runs normal. But for tests souch Hibernate configuration is not applicable.
Is any way for this case to specify different configuration file for JUnit tests?
Best regards.
put the test file in src/test/resources and that will take precedence in classpath while executing tests
I am having trouble figuring out how to use Hibernate Envers and Vaadin. I have a vaadin web application which has tables of information which users can edit. I am using a postgresql to store the data, and have used Hibernate to create the Bean classes (I have multiple Bean classes and multiple SQL tables) and linking to the DB. I am trying to figure out how to use Envers to keep track of user modifications to the table (if they added something, deleted, or edited and the username of the user and the time at which it happened). Here is my hibernate.cfg.xml file:
<?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 name="">
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/appdb</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping
resource="com/webapp/ui/database/Person.hbm.xml" />
<mapping
resource="com/webapp/ui/database/Occupation.hbm.xml" />
<mapping
resource="com/webapp/ui/database/Address.hbm.xml" />
<mapping
resource="com/webapp/ui/database/Car.hbm.xml" />
<mapping
resource="com/webapp/ui/database/Education.hbm.xml" />
<listener class="org.hibernate.envers.event.AuditEventListener" type="post-insert"/>
<listener class="org.hibernate.envers.event.AuditEventListener" type="post-update"/>
<listener class="org.hibernate.envers.event.AuditEventListener" type="post-delete"/>
<listener class="org.hibernate.envers.event.AuditEventListener" type="pre-collection-update"/>
<listener class="org.hibernate.envers.event.AuditEventListener" type="pre-collection-remove"/>
<listener class="org.hibernate.envers.event.AuditEventListener" type="post-collection-recreate"/>
</session-factory>
</hibernate-configuration>
I also added the #Audited to the Bean classes. After doing this it does not seem to work, the audit tables are not being created.