Hibernate No #NamedStoredProcedureQuery was found with that name - java

I am trying to call on a stored procedure from a db2 database using hibernate's entity manager and return the results as an object. I am using the #NamedStoredProcedureQuery annotation which does not seem to be found by hibernate. I am getting the error: "No #NamedStoredProcedureQuery was found with that name : Create_Division". Any ideas would be great.
beans:
<bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="demoJPAUnit" />
<property name="packagesToScan">
<list>
<value>com.merchantBoarding.db2.queryObjects</value>
</list>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.archive.autodetection">class,hbm</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryBean" />
</bean>
Query output object:
#NamedStoredProcedureQuery(
name="Create_Division",
procedureName="MD.PMDBD017",
resultClasses = {CreateDivisionResult.class},
parameters={
#StoredProcedureParameter(name="IN_ENTY",mode=ParameterMode.IN,type=Integer.class),
#StoredProcedureParameter(name="IN_PARNT_ENTY",mode=ParameterMode.IN,type=Integer.class),
#StoredProcedureParameter(name="IN_ACTION",mode=ParameterMode.IN,type=String.class),
#StoredProcedureParameter(name="IN_ENTY_XREF",mode=ParameterMode.IN,type=String.class),
#StoredProcedureParameter(name="OUT_ENTY",mode=ParameterMode.OUT,type=Integer.class),
#StoredProcedureParameter(name="OUT_ID",mode=ParameterMode.OUT,type=String.class),
#StoredProcedureParameter(name="OUT_ENTY_XREF",mode=ParameterMode.OUT,type=String.class),
#StoredProcedureParameter(name="OUT_SQLCODE",mode=ParameterMode.OUT,type=Integer.class),
#StoredProcedureParameter(name="OUT_STATUS",mode=ParameterMode.OUT,type=String.class),
#StoredProcedureParameter(name="OUT_ERRORTEXT",mode=ParameterMode.OUT,type=String.class),
}
)
public class CreateDivisionResult implements Serializable {
#Id
public String OUT_ENTY;
public String OUT_ID;
public String OUT_ENTY_XREF;
public String OUT_SQLCODE;
public String OUT_STATUS;
public String OUT_ERRORTEXT;
}
DAO code:
StoredProcedureQuery query = manager.createNamedStoredProcedureQuery("Create_Division");
query.setParameter("IN_ENTY", 111);
query.setParameter("IN_PARNT_ENTY", 11111);
query.setParameter("IN_ACTION", "CREATE");
query.setParameter("IN_ENTY_XREF", "ED");
List<CreateDivisionResult> results = query.getResultList();
EDIT:
So I added #Entity and that did fix the original error, but the output parameters still aren't mapping to the CreateDivisionResult object. I can only get each indiviual field with query.getOutputParameterValue. Is this just a restriction of JPA?

#NamedStoredProcedureQuery should be applied to Entity class or mapped class
Seems you are missing the #Entity in your 'CreateDivisionResult'

Related

Javax Persistence Error : Unknown Entity com.samplewebentities.Customer

I'm trying to persist an entity using EntityManagerFactory defined in my bean. The function looks like this:
private BaseMasterEntity saveEntity(BaseMasterEntity entity){
EntityManagerFactory emf = (EntityManagerFactory)context.getBean("entityManagerFactory");
EntityManager sf = emf.createEntityManager();
sf.getTransaction().begin();
sf.persist(entity);
sf.getTransaction().commit();
sf.close();
return entity;
}
The problem here is when it persists it cannot find the entity. The entity has #Entity defined clearly with the javax.persistance annotation. This is how my context file looks btw:
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="*my jdbc setting*" />
<property name="username" value="hr" />
<property name="password" value="hr" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
<property name="packagesToScan"
value="classpath*:com.samplewebentities"></property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="hibernateJpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
<tx:annotation-driven transaction-manager="transactionManager" />
If it helps, the classpath*:com.samplewebentites is a different component (The application is a combination of many different components/projects: Using SCA here).
No need for classpath
..
<property name="packagesToScan"
value="com.samplewebentities"></property>
..

Is it possible to specify schema_name during instantiation?

I'm using Spring 2.5.5 and Hibernate 3.2.1.
I have the following entity:
#Entity
#Table(schema = "mailing", name = "mailing")
public class Mailing {
//Fields, GET, SET
}
The issue is I need to use that entity in more than one project and in some of those projects we should specify defferent schema_name. I'd like to put this entity class in the common place in order to avoid code-duplicating. For instance in the Project A schema name is commmon_maling, but in Project B schema name is just mailing. Is it possible to specify the schema name in the springApplicationContext.xml where we're defining the SessionFactory bean like the following:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<!-- Some classes -->
</list>
</property>
</bean>
You can define the hibernateProperties property and add schema inside, e.g.
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<!-- Some classes -->
</list>
</property>
<property name="hibernateProperties">
<props>
...
<property name="hibernate.default_schema">yourschema</property>
...
</props>
</property>
</bean>

How to configure package level #TypeDefs with Spring Hibernate

I need to configure #TypeDefs for use custom #Type on package level. When I configured it following manner I am getting ClassNotFoundException. But when I put #TypeDefs on the class level it is working fine.
I have found similar kind of stackoverflow post but I haven't any idea to how to configure <resource package="com.foo.bar.thepackage"/> entry with my application-context.xml file.
According some post (as bellow), noted this a Spring related bug
I believe this is due to a bug in Spring, where it doesn't scan the annotations in package-info, but only those on classes annotated with #Entity, #Embeddable, or #MappedSuperclass. See https://jira.springsource.org/browse/SPR-8589.
Can any one help me to resolve this issue. Thanks.
#TypeDefs declaration with package-info.java
#TypeDefs
({
#TypeDef(
name="encryptedString",
typeClass=EncryptedStringType.class,
parameters={
#Parameter(name="encryptorRegisteredName",
value="abcHibernateStringEncryptor")
}
)
})
package com.abc.core.model;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;
import org.hibernate.annotations.Parameter;
import org.jasypt.hibernate4.type.EncryptedStringType;
application-context.xml
<!-- ############################### General Configurations #################################################### -->
<!-- Enable annotation-driven configuration and auto-detection -->
<context:annotation-config/>
<!-- Enable Spring configuration-detection -->
<context:spring-configured/>
<!-- Scans the classpath of this application for #Components to deploy as beans -->
<context:component-scan base-package="com.abc"/>
<!-- Configure property placeholders for environment-specific properties customization -->
<context:property-placeholder ignore-resource-not-found="true" location="classpath*:/project.properties"/>
<!-- ############################### Persistence Related Configurations ######################################## -->
<!-- JPA adapter -->
<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
</bean>
<!-- EntityManager factory -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
<property name="packagesToScan" value="com.abc.core.model" />
<property name="jpaProperties">
<props>
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgresPlusDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.use_sql_comments">false</prop>
<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
<prop key="hibernate.connection.autocommit">false</prop>
</props>
</property>
</bean>
<!-- Transaction manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- JPA repositories -->
<jpa:repositories base-package="com.abc"
transaction-manager-ref="transactionManager"
entity-manager-factory-ref="entityManagerFactory"/>
<!-- Use #Transactional annotation on methods -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<!-- ## JASYPT related configurations ## -->
<!-- jasypt encryptor for string -->
<bean id="abcStringEncryptor"
class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
<property name="algorithm">
<value>PBEWithMD5AndDES</value>
</property>
<property name="password">
<value>XXX</value>
</property>
</bean>
<!-- hibernate encryptor for string -->
<bean id="theHibernateStringEncryptor"
class="org.jasypt.hibernate4.encryptor.HibernatePBEStringEncryptor">
<property name="registeredName">
<value>abcHibernateStringEncryptor</value>
</property>
<property name="encryptor">
<ref bean="abcStringEncryptor" />
</property>
</bean>
Entity class with custom #Type mapping
package com.stee.rcm.core.model;
#Entity
#Table(name = "trail")
public class Trail implements Serializable {
#Id
#GeneratedValue
#Column(name = "ID")
private Integer id;
#Type(type="encryptedString")
#Column(name = "DESCRIPTION")
private String description;
}
Exception
Caused by: java.lang.ClassNotFoundException : encryptedString
I too faced same issue, It seems spring have some issue to scan TypeDef when they are at package-info.java class.
Declaring resource in the configuration is nothing but packagesToScan.You have already used in your configuration and even this does not help in scanning TypeDefs.
<resource package="com.foo.bar.thepackage"/>
We can go with any of below approaches
Declare TypeDef in each pojo classes(where ever userType/CustomType is used).
Explicitly register CustomType to hibernate configuration.This can be done in two ways:
Registering CustomType programmatic.
configuration.registerTypeOverride(new EncryptedStringType(),new String[]{"encryptedString"});
Declaring TypeDef in session factory bean configuration and extending local session factory bean.
Spring.xml :
<bean id="DateTypeSessionFactory" class="com.runtime.data.factory.TypeResolverSessionFactoryBean">
<property name="dataSource" ref="DateTypeDataSource"/>
<property name="packagesToScan" value="com.p27.datetype"/>
<property name="customTypes">
<array value-type="com.runtime.data.CustomType">
<ref bean="dateTimeCustomType"/>
</array>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${DateType.dialect}
</prop>
<prop key="hibernate.show_sql">
false
</prop>
<prop key="hibernate.globally_quoted_identifiers">
false
</prop>
<prop key="hibernate.hbm2ddl.auto">
${DateType.hbm2ddl}
</prop>
</props>
</property>
</bean>
<bean id="persistentLocalDateTimeType" class="com.studio.common.data.type.WMPersistentLocalDateTime"/>
<bean id="dateTimeCustomType" class="com.runtime.data.CustomType">
<property name="type" ref="persistentLocalDateTimeType"/>
<property name="keys">
<list>
<value>DateTime</value>
<value>org.joda.time.LocalDateTime</value>
</list>
</property>
</bean>
public class CustomType {
private UserType type;
private String[] keys;
//getters & setters
}
SessionFactoryBean :
public class TypeResolverSessionFactoryBean extends LocalSessionFactoryBean {
private CustomType[] customTypes;
#Override
protected SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sfb) {
if(customTypes != null) {
registerCustomTypes(sfb);
}
return sfb.buildSessionFactory();
}
protected void registerCustomTypes(LocalSessionFactoryBuilder sfb) {
for (CustomType customType : customTypes) {
sfb.registerTypeOverride(customType.getType(), customType.getKeys());
}
}
public void setCustomTypes(CustomType[] customTypes) {
this.customTypes = customTypes;
}
}

MySQLSyntaxErrorException in Hibernate My SQL version

i have the following hibernate configuration:
<bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource" />
<property name="annotatedClasses">
<list>
<value>com.google.hacatone.entity.MainCategory</value>
<value>com.google.hacatone.entity.GeorgianGrapes</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
when i try to select data from table i get the following error:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'georgiangr0_' at line 1
please help me how to fix this problem, i try to use any mysql driver but problem not change
#Entity
#Table(name = "main_category", catalog = "hacatone_db")
public class MainCategory implements java.io.Serializable {
}
#Entity
#Table(name = "georgian_ grapes", catalog = "hacatone_db")
public class GeorgianGrapes implements java.io.Serializable {
}
this is my entity, how to force hibernate to generate right query?

Joda type def loading fails during hibernate init

Joda time in pair with hibernate support is used. Configuration is as follows:
There's typedefs in org.joda.time.package-info.java:
#org.hibernate.annotations.TypeDefs({
#org.hibernate.annotations.TypeDef(
name="localDate",
typeClass =
org.joda.time.contrib.hibernate.PersistentLocalDate.class
)
})
package org.joda.time;
There's a spring context with session factory config:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="packagesToScan">
<list>
<value>org.joda.time</value>
</list>
</property>
<property name="annotatedClasses">
<list>
<value>...</value>
...
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.query.substitutions">true 1, false 0, yes 'Y', no 'N'</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="dataSource" ref="dataSource"/>
</bean>
Then there's test case:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration({"classpath:spring-test.xml"})
#Transactional
public class OperatorDaoTest extends AbstractTransactionalJUnit4SpringContextTests {
//autowired dao field defined
...
#Test
public void testMethod(){
//calls DAO method
}
}
Problem is in next exception:
Caused by: org.hibernate.MappingException: Could not determine type for: localDate, at table: TABLE_NAME, for columns: [org.hibernate.mapping.Column(DATE_COLUMN)]
I use standard Date to map to database, and then in my getter/setter I use joda-time and perform the conversion, to avoid issues like you are having. This may be of use.

Categories