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;
}
Related
My application is experiencing a strange issue where sometimes it blocks forever when attempting to run an SQL query against the database, even if that is a small select statement on one single row.
This may occur at different points in the application, whenever it tries do execute a query (SELECT, UPDATE, etc.), which indicates this is perhaps a configuration issue.
With a tcpdump I was able to tell the query is never actually sent to the Oracle database, despite hibernate show_sql saying otherwise.
The worst thing is that as soon as the application blocks on a query for the first time, any other database queries in that machine (on different Threads) block in the same way, even though the machine is not nearly at its load capacity and can do any non-database work.
The only solution to this has been restarting the application, which runs as expected (with SQL queries) until at some random point in time (even days after) it blocks forever again...
This runs on
Hibernate 5.2.9.Final
Apache camel 2.19.2
applicationContext.xml
<!-- JPA Setup -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="myPersistenceUnit" />
<property name="persistenceUnitPostProcessors">
<list>
<bean class="test.package.ReportingDbConnSetup" />
</list>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="false" />
</bean>
</property>
</bean>
<bean id="jpa" class="org.apache.camel.component.jpa.JpaComponent">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="transactionTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager">
<bean class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</property>
</bean>
persistence.xml
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="jboss.as.jpa.managed" value="false" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver" />
<property name="org.hibernate.flushMode" value="ALWAYS" />
<!-- username, password and connection url hidden -->
<property name="hibernate.hbm2ddl.auto" value="" />
<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JDBCTransactionFactory" />
<property name="hibernate.current_session_context_class" value="thread" />
<property name="connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />
<property name="hibernate.c3p0.acquire_increment" value="1" />
<property name="hibernate.c3p0.idle_test_period" value="60" />
<property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.max_size" value="12" />
<property name="hibernate.c3p0.max_statements" value="35" />
<property name="hibernate.c3p0.timeout" value="120" />
<property name="hibernate.c3p0.acquireRetryAttempts" value="3" />
<property name="hibernate.c3p0.acquireRetryDelay" value="210" />
</properties>
Here is an example code block that might hang forever with no additional information:
#PersistenceUnit(unitName = "myPersistenceUnit")
private EntityManagerFactory entityManagerFactory;
public void saveOrUpdate(Object o){
EntityManager em = null;
try{
em = entityManagerFactory.createEntityManager();
Session session = em.unwrap(Session.class);
txn = session.beginTransaction();
session.saveOrUpdate(o);
txn.commit();
}
catch(Exception e){
if (txn !=null && txn.isActive()) {
txn.rollback();
}
LOG.error(e);
}
finally{
if(em != null && em.isOpen()){
try{
em.close();
} catch (Exception e){
LOG.error(e);
}
}
}
}
Is it possible to use executeUpdate for createNativeQuery on openjpa slice?
We tried something like this but that doesn’t update any record. We are using
MySQL database. Log shows that updates go to both slices and are immediately rollbacked. Everything works fine without slice.
String updateQueryString = "update address set name = ?1 ";
Query query = JPA.em().createNativeQuery(updateQueryString)
.setHint("openjpa.hint.slice.Target", “One”)
.setParameter(1, "adr1");
query.executeUpdate();
Our persistence.xml file:
<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="slice">
<provider>
org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>
<class>controllers.Address</class>
<class>controllers.User</class>
<class>models.Account</class>
<class>controllers.CostCenter</class>
<properties>
<property name="openjpa.BrokerFactory" value="slice"/>
<property name="openjpa.slice.Names" value="One, Two"/>
<property name="openjpa.slice.Master" value="One"/>
<property name="openjpa.ConnectionUserName" value="*****"/>
<property name="openjpa.ConnectionPassword" value="******"/>
<property name="openjpa.ConnectionDriverName"
value="org.apache.openjpa.slice.jdbc"/>
<property name="openjpa.slice.DistributionPolicy"
value="sharding.RublysDistributionPolicy"/>
<property name="openjpa.slice.QueryTargetPolicy "
value="sharding.RublysQueryTargetPolicy"/>
<property name="openjpa.slice.FinderTargetPolicy"
value="sharding.RublysTargetPolicy" />
<property name="openjpa.slice.ReplicatedTypes"
value="models.Account,controllers.CostCenter"/>
<property name="openjpa.slice.One.ConnectionURL"
value="jdbc:mysql://localhost:3306/slice1"/>
<property name="openjpa.slice.One.ConnectionDriverName"
value="com.mysql.jdbc.Driver"/>
<property name="openjpa.slice.Two.ConnectionURL"
value="jdbc:mysql://localhost:3306/asrtest2"/>
<property name="openjpa.slice.Two.ConnectionDriverName"
value="com.mysql.jdbc.Driver"/>
<property name="openjpa.DynamicEnhancementAgent"
value="false"/>
<property name="openjpa.Log"
value="SQL=TRACE, Runtime=TRACE, DefaultLevel=TRACE ,Tool=TRACE"/>
<property name="openjpa.Multithreaded" value="true"/>
<property name="openjpa.jdbc.SynchronizeMappings" value="refresh"/>
<property name="openjpa.jdbc.MappingDefaults" value="DefaultMissingInfo=true"/>
<property name="openjpa.jdbc.DBDictionary" value="mysql"/>
<property name="openjpa.QueryCache" value="false"/>
<property name="openjpa.DataCache" value="false"/>
<property name="openjpa.QueryCompilationCache" value="false"/>
<property name="openjpa.jdbc.QuerySQLCache" value="false"/>
<property name="openjpa.jdbc.FinderCache" value="false"/>
<property name="openjpa.DetachState" value="loaded(DetachedStateField=false)"/>
<property name="openjpa.slice.ThreadingPolicy" value="fixed"/>
</properties>
</persistence-unit>
</persistence>
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
I'm trying to make an application with Spring 3, JPA 2 and Hibernate 3.
I have a problem when y persist an entity : nothing happen ! Data are not inserted in database, and not query is executed.
But when i'm using a request like query.getResultList() a select works correctly.
So I think my problem is only on a persist/update and on the transaction manager but i'm not really good with spring.
Can you help me please ?
Here are my configuration files :
My applicationContext.xml
<jee:jndi-lookup id="soireeentreamis_DS" jndi-name="jdbc/soireeentreamis" />
<bean id="persistenceUnitManager"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
<property name="defaultDataSource" ref="soireeentreamis_DS" />
<property name="dataSources">
<map>
<entry key="soireeentreamisDS" value-ref="soireeentreamis_DS" />
</map>
</property>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager" />
<property name="persistenceUnitName" value="soireeentreamisPU" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
</bean>
</property>
</bean>
<bean id="soireeentreamisTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<tx:annotation-driven transaction-manager="soireeentreamisTransactionManager" />
<context:annotation-config />
</beans>
My persistence.xml
<persistence-unit name="soireeentreamisPU"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>soireeentreamisDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
My service
#Service
#Transactional("soireeentreamisTransactionManager")
public class UserServiceImpl implements UserService ...
My dao
#Repository
public class UserDaoImpl extends GenericDaoImpl<User, String> implements
UserDao {
#PersistenceContext(unitName = "soireeentreamisPU")
private EntityManager em;
public void persist(final User entity) {
em.persist(entity);
}
}
Can somebody help me please?
I find similar problem a while ago. In my case I needed to add line below to my dispacher-servlet.xml. So I need this in 2 places (applicationContex.xml and dispacher-servlet.xml)
<tx:annotation-driven />
And to clear something out, you didn't show your service methode that "store" object, but I believe that it's annotated with #Transactional - cause wihout this one you want create new transaction.
This happened to me recently. The problem is as you say a transactional problem, add the #Transactional annotation to all the methods that update the DB. Then add the CGLIB library to your calsspath or add it to your pom.xml if you are using Maven, this is necessary for spring make transactions. Even if I did it on a different way I hope it can help you. Here is my db.xml where I have all the data base related spring configuration.
<!-- Scans within the base package of the application for #Components to configure as beans -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="${db.dialect}" />
</bean>
</property>
</bean>
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
destroy-method="close">
<property name="driverClass" value="${db.driver}" />
<property name="jdbcUrl" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="idleConnectionTestPeriodInMinutes" value="1" />
<property name="idleMaxAgeInMinutes" value="4" />
<property name="maxConnectionsPerPartition" value="30" />
<property name="minConnectionsPerPartition" value="10" />
<property name="partitionCount" value="3" />
<property name="acquireIncrement" value="5" />
<property name="statementsCacheSize" value="100" />
<property name="releaseHelperThreads" value="3" />
</bean>
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="jpaDialect" ref="jpaDialect"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
And here is my persistence.xml
<persistence version="2.0"
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">
<persistence-unit name="pu-app" transaction-type="RESOURCE_LOCAL">
</persistence-unit>
You need to add the model object into the persistence.xml file. Right below the provider element add
<class>com.your.domain.object.User</class>
I'm using UserType 3.0.0.RC1 to map JodaMoney to Hibernate.
I'm stuck with an error when the SessionFactory initialises:
PersistentMoneyAmount requires currencyCode to be defined as a
parameter, or the defaultCurrencyCode Hibernate property to be defined
I'm sure I must have some configuration issue — here's the relevant snippets.
Persistence.xml:
<persistence-unit name="spring-jpa">
<properties>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="jadira.usertype.autoRegisterUserTypes" value="true"/>
</properties>
</persistence-unit>
The relevant spring config:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.mangofactory.concorde</value>
<value>com.mangofactory.moolah</value>
</list>
</property>
<property name="persistenceUnitName" value="spring-jpa" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
</bean>
</property>
</bean>
Any tips on what I'm missing?
I ended up solving this using the following configuration in my persistence.xml:
<persistence-unit name="spring-jpa">
<properties>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="jadira.usertype.autoRegisterUserTypes" value="true"/>
<property name="jadira.usertype.currencyCode" value="AUD"/>
<property name="jadira.usertype.seed" value="org.jadira.usertype.spi.shared.JvmTimestampSeed"/>
</properties>
</persistence-unit>
The tricky part is that I needed to provide a jadira.usertype.seed in order for the jadira.usertype.currencyCode to be detected.
if you're looking for an annotation based solution, you may try this
#Type(type = "org.jadira.usertype.moneyandcurrency.joda.PersistentMoneyAmount", parameters = { #Parameter(name="currencyCode", value="USD") })
private Money price;
found here
Email Archive: usertype-discuss (read-only)
Regarding the need to configure seed - this is due to a bug in 3.0.0-RC1 and will be fixed in future.