Changing database in eclipse/hibernate project - java

My peristance file is:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="jpa" transaction-type="RESOURCE_LOCAL">
<!-- provider -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>sau.se.migration.model.TGridStation</class>
<class>sau.se.domain.model.Graph</class>
<class>sau.se.domain.model.Role</class>
<class>sau.se.domain.model.StationPrincipale</class>
<class>sau.se.domain.model.StationSecondaire</class>
<class>sau.se.domain.model.User</class>
<class>sau.se.domain.model.Incident</class>
<class>sau.se.domain.model.Listes</class>
<class>sau.se.domain.model.Vip</class>
<class>sau.se.domain.model.Feeder</class>
<properties>
<!-- Classes persistantes -->
<!--<property name="hibernate.archive.autodetection" value="class, hbm"
/> -->
<!-- logs SQL -->
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="use_sql_comments" value="true" />
<!-- connexion JDBC -->
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost/semap?zeroDateTimeBehavior=convertToNull" />
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.connection.characterEncoding"
value="UTF-8" />
<property name="hibernate.connection.charSet" value="UTF-8" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="123456789" />
<!-- Dialecte -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>
And I am quiet sure that it is the only configuration file in my project because I tried to put wrong password and I got the expected error on connection.
I want to change semap db name to any other but in vain.
Is there any other configuration that I don't know?
UPDATE
The real problem is that my application is still working with semap db (the old one)

Related

JPA/Hibernate: How to scan particular package in Persistence.xml in place of giving single entity name?

I have 10 entities in package:
com.paka.maka.raka
I have 5 entities in package
com.paka
I only want to scan 10 entities in first package name.
Is it mandatory to name whole classes or is there way to give package name only to scan in Persistance.xml
<persistence-unit name="IntegratorMasterdataPU" transaction-type="RESOURCE_LOCAL" >
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.url" value="jdbc:oracle:thin:#dat-esb-orat:1521/rator_test" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver" />
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.connection.username" value="DATA" />
<property name="hibernate.connection.password" value="264rcxx" />
<!--QA <property name="hibernate.connection.password" value="emifuqqgqn5" /> -->
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<!-- <property name="hibernate.hbm2ddl.auto" value="create" /> -->
</properties>
</persistence-unit>
I'm not pretty sure it will work, but you can use:
<property name="packagesToScan" value="com.paka.maka.raka"/>
In your entityManagerFactory bean declaration
Check this post

Spring mvc multiple entities database persistance

I'm having troubles with "working with multiple databases" in Spring MVC - hibernate JPA
I have two databases called user_db and portal_db. I need to work with them in different jpa units.
here is my persistance.xml
<?xml version="1.0" encoding="UTF-8" ?>
<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_1_0.xsd" version="1.0">
<persistence-unit name="user_unit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>Package.Entity1</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost/user_db" />
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.connection.characterEncoding" value="UTF-8" />
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
<property name="hibernate.connection.username" value="postgres" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect" />
<!--<property name="hibernate.hbm2ddl.auto" value="create-drop" />-->
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
<persistence-unit name="portal_unit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>Package.Entity2</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost/portal_db" />
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.connection.characterEncoding" value="UTF-8" />
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
<property name="hibernate.connection.username" value="postgres" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect" />
<!--<property name="hibernate.hbm2ddl.auto" value="create-drop"/>-->
<property name="hibernate.hbm2ddl.auto" value="validate"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
Problem is When I use create-drop and run my project, it creates both of my entites in both databases. Like table that should be only created by Package.Entity1 in user_unit, is also created in portal_unit.
And when I select,insert,update my entites, I set persistance unit in each of my individual DAO's.
for example, in Entity Dao's implementation I have:
EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit);
where persistenceUnit could be user_unit or portal_unit depending on implementation.
What changes should I make so it wont create same table in both databases?
Add <exclude-unlisted-classes>true</exclude-unlisted-classes> in both of yours persistence units OR use JPA 2.x
Add <exclude-unlisted-classes>true</exclude-unlisted-classes> in both your persistence units.
Please follow post below for detailed explanation:
Multiple persistance unit in persistence.xml creating tables in one another

Spring JPA Autoscan entities in jar file

I have two projects:
common and hrm
project common is a parent project(packaged as Jar) and hrm is a dependent project(packaged as war)
Dependency is specified through maven
Maven dependency is specified as:
<dependency>
<groupId>com.talentera</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
My persistence XML in hrm project:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="talentera" transaction-type="RESOURCE_LOCAL">
<description>example of enabling the second level cache.</description>
<jta-data-source>java:jboss/datasources/TalenteraDS</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<jar-file>../../lib/common-0.0.1-SNAPSHOT.jar</jar-file>
<properties>
<property name="hibernate.cache.use_second_level_cache" value="false" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.bytecode.use_reflection_optimizer" value="false" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.use_outer_join" value="false" />
<property name="hibernate.cache.use_structured_entries" value="true" />
<property name="hibernate.generate_statistics" value="true" />
<property name="hibernate.id.new_generator_mappings" value="false" />
<property name="hibernate.default_batch_fetch_size" value="500" />
<property name="hibernate.max_fetch_depth" value="5" />
<property name="hibernate.jdbc.batch_size" value="1000" />
<property name="jboss.as.jpa.managed" value="false" />
<property name="hibernate.archive.autodetection" value="class, hbm"/>
</properties>
</persistence-unit>
</persistence>
Spring configuration:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="appDataSource" />
<property name="persistenceUnitName" value="talentera" />
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="showSql" value="true" />
</bean>
</property>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="appDataSource" />
</bean>
Please help me to make the entities available in common to be auto recognized in hrm project by spring-jpa
common is packaged as Jar and hrm is packaged as war.
Please let me know if I need to provide any more information here.
I had your same problem with a composite unit, I solved in this way:
you have to change
<jar-file>../../lib/common-0.0.1-SNAPSHOT.jar</jar-file>
with
<jar-file>WEB-INF/lib/common-0.0.1-SNAPSHOT.jar</jar-file>
ii should work

how to make ha-jdbc datasource work

i have a ha-jdbc xml where i have this configuration:
<ha-jdbc>
<sync id="diff" class="net.sf.hajdbc.sync.DifferentialSynchronizationStrategy">
<property name="fetchSize">1000</property>
<property name="maxBatchSize">100</property>
</sync>
<sync id="full" class="net.sf.hajdbc.sync.FullSynchronizationStrategy">
<property name="fetchSize">1000</property>
<property name="maxBatchSize">100</property>
</sync>
<cluster balancer="load"
dialect="net.sf.hajdbc.dialect.MySQLDialect"
default-sync="full"
transaction-mode="parallel"
auto-activate-schedule="0 * * ? * *"
failure-detect-schedule="0 * * ? * *"
meta-data-cache="none">
<database id="database1">
<driver>****</driver>
<url>****</url>
<user>****</user>
<password>****</password>
</database>
<database id="database2">
<driver>****</driver>
<url>****</url>
<user>****</user>
<password>****</password>
</database>
</cluster>
</ha-jdbc>
i am accessing ha-jdbc using jpa config:
<?xml version="1.0" encoding="UTF-8"?>
<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_1_0.xsd"
version="1.0">
<persistence-unit name="app_data" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="net.sf.hajdbc.sql.Driver" />
<!--property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/-->
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />
<property name="hibernate.connection.url" value="jdbc:ha-jdbc:cluster1" />
<property name="hibernate.connection.username" value="****" />
<property name="hibernate.connection.password" value="****" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<!--property name="hibernate.hbm2ddl.auto" value="update" /-->
<property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.max_size" value="20" />
<property name="hibernate.c3p0.timeout" value="1800" />
<property name="hibernate.c3p0.max_statements" value="50" />
<property name="hibernate.c3p0.idle_test_period" value="3000" />
</properties>
</persistence-unit>
when i run maven i get the following error:
org.jibx.runtime.JiBXException: Expected "cluster" end tag, found "datasource" start tag (line 19, col 11)
could anyone explain what can be the solution?

"Unknown database 'mydb' " error with jpa

i'm trying to connect to mysql server but get this error. when mydb is the only name in the system. please tell me where i'm wrong...
my persistence xml is as folow:
<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_1_0.xsd"
version="1.0">
<persistence-unit name="MyPersistenceUnit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/mydb" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="tft" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.use_sql_comments" value="true" />
</properties>
</persistence-unit>
</persistence>
1.You have to check your mysql jdbc driver
2.You have to check mysql server state and validation of username & password.

Categories

Resources