Hibernate don't update entity after changing something directly in database? - java

Hi all I have pretty strange problem let me first show my configuration etc. Here is persitance.xml:
<persistence-unit name="allegroTransactionPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/AllegroShop?UseUnicode=true&characterEncoding=utf8" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.username" value="topSecret" />
<property name="hibernate.connection.password" value="topSecret" />
<!-- <property name="hibernate.hbm2ddl.auto" value="create" /> -->
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="show_sql" value="true" />
<property name="hibernate.hbm2ddl.import_files" value="/SQL/payment_type.sql"/>
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.connection.characterEncoding" value="UTF-8" />
<property name="hibernate.connection.charSet" value="UTF-8" />
</properties>
</persistence-unit>
And here is method that I use to take data from my database:
#PersistenceContext( unitName = "allegroTransactionPersistenceUnit", type= PersistenceContextType.EXTENDED )
protected EntityManager em;
public List<AllegroTransactionImpl> readAllegroTransactionByCreateDate()
{
TypedQuery<AllegroTransactionImpl> query = this.em.createQuery( "SELECT allegroTransaction FROM com.springapp.mvc.classes.AllegroTransactionImpl allegroTransaction ORDER BY createDate DESC", AllegroTransactionImpl.class);
return query.getResultList();
}
And now the problem. Everything works great if I use hibernate to update / delete / add. but if I change some value directly in base (by SQL statement) hibernate don't refresh it question is why ?
Entity class
https://gist.github.com/spec8320/144100f049f54b73ad86

please make sure you had committed the change by SQL statement.
please check if you have enabled the 2nd level cache on AllegroTransactionImpl

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 | Hibernate - Returns only 1000 entities

I'm new in Java Web Development and for last several days I've been having a big problem, I can not make JPA/Hibernate to return more than 1000 entities.
In my database I have around 1600 Assortment entities but whatever I try, I can't get more than 1000 of them, even when I try to count them so I would be able to paginate requests, COUNT(...) returns 1000.
I know that problem is not on database server because I've already made PHP server side scripts that work flawlessly with this SQL server.
I've tried using Query, NativeQuery and Criteria. But nothing returns more than 1000 entities.
I'm sure I googled everything, and really do not have any idea what else to do, please help.
My code is below and thank you in advance!
spring-context.xml
...
<!-- SQL Server Datasource with Commons DBCP connection pooling -->
<bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://XXX;database=XXX;user=XXX;password=XXX;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;" />
<property name="validationQuery" value="select 1" />
<property name="validationQueryTimeout" value="1000" />
<property name="testOnBorrow" value="true" />
</bean>
<!-- EntityManagerFactory -->
<bean
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
id="entityManagerFactory">
<property name="persistenceUnitName" value="persistenceUnit" />
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Transaction Manager -->
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
...
persistence.xml
<persistence xmlns="..." xmlns:xsi="..." version="2.0" xsi:schemaLocation="...">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2008Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="validate"/>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
<property name="hibernate.connection.charSet" value="UTF-8"/>
<property name="hibernate.generate_statistics" value="true"/>
<property name="hibernate.show_sql" value="false"/>
</properties>
</persistence-unit>
</persistence>
AssortmentService.java
#Transactional
public List<Assortment> getWholeAssortment() {
String searchQuery = "SELECT a FROM Assortment a";
List<Assortment> result = entityManager.createQuery(searchQuery, Assortment.class).getResultList();
return result;
}
#Transactional
public int getAssortmentCount(){
String searchQuery = "SELECT COUNT(1) FROM test_assortment";
int result = (Integer) entityManager.createNativeQuery(searchQuery).getSingleResult();
return result;
}
#Transactional
public List<Assortment> getAssortmentWithCriteria(){
CriteriaQuery<Assortment> criteria = entityManager.getCriteriaBuilder().createQuery(Assortment.class);
criteria.select(criteria.from(Assortment.class));
List<Assortment> result = entityManager.createQuery(criteria).getResultList();
return result;
}

unit testing: jpa, hibernate - how to handle entity managers?

I have a test class for a programme that uses jpa/hibernate, the dao pattern, controller classes and a gui - the usual.
I have written a persistence.xml that contains persistence-units for both the main and the test classes:
<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_2_0.xsd"
version="2.0">
<persistence-unit name="com.to.me.project.main"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/database" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.id.new_generator_mappings" value="true"/>
<!-- <property name="hibernate.generate_statistics" value="true" /> -->
</properties>
</persistence-unit>
<persistence-unit name="com.to.me.project.test"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:unit-testing" />
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
Works fine in production code and when I run the test, the right persistence configuration is found and the databases are created.
My programme works like this: the controller calls a method of the DAO (such as create) and in this method, I get the entitymanager and use its methods (such as persist):
#Override
public void create(final SomeEntity object) throws DAOException {
EntityManager em = MyPersistenceManager.getEntityManager();
em.getTransaction().begin();
em.persist(objekt);
em.getTransaction().commit();
em.close();
}
(The PersistenceManager is my own class, a wrapper around the EntityManagerfactory and so on...)
The problem: That entity manager uses the persistence-unit configs for the main-classes, i. e. the mysql database.So when I run my test and call the controller, it doesnt use the hsqldb databases I set up.
How do I test my controller logic with the configurations for the test persistence unit, i. e. the hsqldb?

Check For Unmapped Columns In Hibernate Mapping

I'm writing a database synchronization tool for existing databases. One of the requirement is to validate the entities with the database schema. The program has to be aware of any database changes (like adding new columns to the database). I'm planning on using Hibernate for this project.
Is there anyway Hibernate can validate the difference between the entity and the table in the database?
Can Hibernate output SQL script so I can review the SQL script before running it to update the target database?
Yes hibernate can output the SQL in the logs.
You need to set the : - <property name="hibernate.show_sql" value="true"/>
but SQL generated by the server is little messy so you have to modify the column name as per your database table.
please have a look at the following XML of my project with the hibernate
<?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_2_0.xsd"
version="2.0">
<persistence-unit name="communityPortalPersitenceUnit">
<class>com.googlecode.frameworksintegration.domain.Blog</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/conhint_prod" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="admin" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.connection.zeroDateTimeBehavior" value="convertToNull"/>
<property name="hibernate.show_sql" value="true"/>
<!--Start :- connection pooling -->
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />
<property name="hibernate.c3p0.max_size" value="100" />
<property name="hibernate.c3p0.min_size" value="0" />
<property name="hibernate.c3p0.acquire_increment" value="1" />
<property name="hibernate.c3p0.idle_test_period" value="300" />
<property name="hibernate.c3p0.max_statements" value="0" />
<property name="hibernate.c3p0.timeout" value="100" />
<!--End :- connection pooling -->
</properties>
</persistence-unit>
</persistence>

Categories

Resources