I am trying to create two join tables ConceptModelDetails and Instructions using a foreign key. Following are my model classes:
ConceptModelDetails:
package com.assignment.model;
#Entity
#Table(name="conceptModelDetails")
public class ConceptModelDetails {
#Id #GeneratedValue(strategy=GenerationType.AUTO)
private int instructionsId;
private String operationType;
private String conceptModelID;
private String requestor;
private String status;
private Timestamp requestDateTime;
private Timestamp lastExecutedDateTime;
private Timestamp completedDateTime;
#OneToMany(cascade=CascadeType.ALL, mappedBy="conceptModelDetails")
private Set<Instructions> instructions;
public ConceptModelDetails() {}
}
and Instuctions:
package com.assignment.model;
#Entity
#Table(name="instructions")
public class Instructions {
#Id
#GeneratedValue
private int Sno;
private String instruction;
#ManyToOne
#JoinColumn(name="instructionsId")
private ConceptModelDetails conceptModelDetails;
}
Following is applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://192.168.1.79:5432/test" />
<property name="username" value="postgres" />
<property name="password" value="admin" />
</bean>
<bean id="objDAO" class="com.assignment.dao.impl.ConceptModelDAOImpl">
<property name="sessionFactory" ref="sessionfactory" />
</bean>
<bean id="sessionfactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan" value="com.assignment.model"></property>
<property name="annotatedClasses">
<list>
<value>com.assignment.model.ConceptModelDetails</value>
<value>com.assignment.model.Instructions</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionfactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
and Controller:
#RequestMapping(value = "/myCntrl", method = RequestMethod.POST)
public String handler(HttpServletRequest request) {
System.out.println("handler");
// System.out.println(request.getParameter("conceptID"));
// System.out.println(request.getParameter("operationType"));
String[] operations = request.getParameterValues("operations");
Date date = new Date();
Timestamp time = new Timestamp(date.getTime());
ConceptModelDetails conceptModelDetails = new ConceptModelDetails();
conceptModelDetails
.setConceptModelID(request.getParameter("conceptID"));
conceptModelDetails.setOperationType(request
.getParameter("operationType"));
conceptModelDetails.setRequestor(request.getParameter("requestor"));
conceptModelDetails.setRequestDateTime(time);
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
System.out.println("yo");
ConceptModelDAO obj = (ConceptModelDAO)context.getBean("objDAO");
System.out.println("no");
Instructions instructions = new Instructions();
for(int i = 0; i < operations.length; i++){
instructions.setInstruction(operations[i]);
obj.addInstructions(instructions);
}
obj.add(conceptModelDetails);
return "success";
}
Problems when I run this code are:
Same hibernate_sequence is used for both the tables.
Foreign key is not mapped in the Instruction table as seen in the following screenshot.
Please guide what is wrong with the code. I am new to hibernate and spring, so I'd appreciate a detailed explanation. Thanks in advance.
1) You are using the Global sequence generator that hibernate provide by default when no generator is provided as specifed by the JPA Spec.
Change it as follows. Do this to the both of the classes with different sequences for each class.
#Entity
#SequenceGenerator(name="PRIVATE_SEQ", sequenceName="private_sequence")
public class ConceptModelDetails {
#Id #GeneratedValue(strategy = GenerationType.SEQUENCE, generator="PRIVATE_SEQ")
private int instructionsId;
2) Here Instructions class owns the relationship and
therefore you should set ConceptModelDetails object to the Instructions object before saving the Instructions object.
ConceptModelDetails cmd1 = new ConceptModelDetails();
Instructions i = new Instructions()
i.setConceptModelDetails(cmd1);
....
Then save the Instructions object i
Hope this helps.
Related
I have three entities (there're also getters and setters for every field):
public class IndicatorSet {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
#NaturalId
#Column(unique=true, nullable=false)
private String code;
#Column(nullable=false)
private String name;
#OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
#JoinTable(
name = "indicator_set_indicators",
joinColumns = #JoinColumn(name = "indicator_set_id"),
inverseJoinColumns = #JoinColumn(name = "indicator_id")
)
private List<Indicator> indicators;
}
public class Indicator {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
#Column(nullable=false, unique=true)
private String code;
}
public class IndicatorSetIndicator {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
#ManyToOne
#JoinColumn(name="indicator_id", nullable=false)
private Indicator indicator;
#ManyToOne
#JoinColumn(name="indicator_set_id", nullable=false)
private IndicatorSet indicatorSet;
}
I put in database an IndicatorSet, Indicators and connect it with IndicatorSetIndicator table on my server and in junit test before running the following code.
IndicatorSet set = indicatorSetDAO.get(idOfSet);
List<Indicator> indicators = set.getIndicators();
When I invoke this code on server it works and I see my Indicators in list.
When I try to invoke this code in junit test, I see in list null reference.
And that's strange.
Also when I invoke the following code in junit test, it works perfect and indicators appears in list:
IndicatorSet set = indicatorSetDAO.get(idOfSet);
List<Indicator> indicators = indicatorSetIndicatorDAO.getIndicatorsBySet(set);
DAO uses Criteria API to get list of indicators.
There're this set of annotations on class with tests:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration("classpath:TestContext.xml")
#Transactional
#TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class, SessionRequestTestExecutionListener.class })
TestContext.xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans [schemes]>
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="org.example.monitoring" />
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<jdbc:embedded-database id="dataSource" type="HSQL" />
<bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">
<property name="dataSource" ref="dataSource" />
<property name="changeLog" value="classpath:db-changelog.xml" />
<property name="contexts" value="test" />
<!-- <property name="dropFirst" value="true" /> -->
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name='packagesToScan' value='pro.sisit.etalon.monitoring.entities' />
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.connection.autocommit">true</prop>
</props>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="request">
<bean class="org.springframework.web.context.request.RequestScope" />
</entry>
<entry key="session">
<bean class="org.springframework.web.context.request.SessionScope" />
</entry>
</map>
</property>
</bean>
</beans>
Configuration of Hibernate not in test enviroment:
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/monitoring" />
</bean>
<bean id='sessionFactory'
class='org.springframework.orm.hibernate4.LocalSessionFactoryBean'>
<property name='dataSource' ref='dataSource' />
<property name='packagesToScan' value='pro.sisit.etalon.monitoring.entities'/>
<property name='hibernateProperties'>
<props>
<prop key='hibernate.dialect'>org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
<prop key='hibernate.show_sql'>false</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
<prop key="hibernate.connection.characterEncoding">UTF-8</prop>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
</props>
</property>
</bean>
How can I make my code work in JUint test?
How can I get #JsonIgnore to work I have a class. And even if I put the annotation there it has no effect on the output. I am using Jackson.
public class QuestionBlock implements ComparableByID{
int ID;
String title;
String description;
boolean deleted;
boolean isDraft;
boolean visible;
Timestamp modifiedDate;
String modifiedBy;
private List<Question> questions = new ArrayList<>();
#JsonIgnore
private List<Survey> surveys = new ArrayList<>();
...
#JsonIgnore
public List<Survey> getSurveys() {
return surveys;
}
#JsonIgnore
public void setSurveys(List<Survey> surveys) {
this.surveys = surveys;
}
}
This is my Controller method:
#RequestMapping(value = "/questionBlock/{id}",produces = "application/json;charset=UTF-8")
#ResponseBody
public QuestionBlock getQuestionBlock(#PathVariable("id") int id) {
return surveyService.getQuestionBlock(id);
}
Here is my servlet-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources location="/, classpath:/META-INF/web-resources/"
mapping="/resources/**" />
<context:property-placeholder location="classpath*:META-INF/*.properties" />
<context:component-scan base-package="com.adam.czibere" />
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="myDataSource" name="dataSource">
<property name="driverClassName" value="${database.driverClassName}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="true" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="1800000" />
<property name="numTestsPerEvictionRun" value="3" />
<property name="minEvictableIdleTimeMillis" value="1800000" />
<property name="validationQuery" value="SELECT 1" />
</bean>
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan">
<array>
<value>com.adam.czibere</value>
</array>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html" />
<entry key="json" value="application/json" />
</map>
</property>
<property name="viewResolvers">
<list>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="prefixJson" value="false" />
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</list>
</property>
</bean>
<bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="prefixJson" value="false" />
<property name="supportedMediaTypes" value="application/json" />
</bean>
</list>
</property>
</bean>
</beans>
I have finally found a solution. I changed the import statement from
import com.fasterxml.jackson.annotate.JsonIgnore; // com. instead of org.
to
import org.codehaus.jackson.annotate.JsonIgnore;
Basically you have to make sure you are using the same class everywhere.
The annotation should only be on the 'get' methods. You seem to have #Json... annotations on your private fields.
If you are using an implementation of Jackson and its annotations are not working it's probably because you have another dependency of jackson with better precedence. Hence if you want to assure that certain implementation of jackson prevales (IMHO the best choice is the one that you have all classes already annotated with, because probably it came with other dependencies) specify this dependency in the pom of the application module. So if you have in multiple modules all your entities annotated with
import com.fasterxml.jackson.annotate.JsonIgnore; // note: com. instead of org.
Instead of replacing all imports just specify the corresponding dependency in the application pom:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
This will clarify to Spring Boot that this is the implementation you want to use.
Putting #JsonProperty(access = JsonProperty.Access.WRITE_ONLY) before the field declaration solved the problem for me.
#JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private List<Survey> surveys = new ArrayList<>();
I'm using jackson version 2.11.3
I added #Getter(onMethod_=#JsonIgnore) for boolean.
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
#Data
#Builder
#NoArgsConstructor
#AllArgsConstructor
#JsonIgnoreProperties(ignoreUnknown = true)
public class TestClass {
#JsonIgnore
private String name;
#Getter(onMethod_=#JsonIgnore)
private boolean isValid;
}
I faced this issue in my project and finally I got the solution.
Replace the dependency...
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.13</version>
</dependency>
Import : com.fasterxml.jackson.annotation.JsonIgnore;
I hope, It will work.
In my case JsonIgnore was not working only on dates. Then I understood that was because of #JsonFormat() that I was using to define the format of date. Removing that solved the issue.
You Need to change your org.codehaus.jackson version.
I am currently using 1.1.1 version in which #JsonIgonre
is not working.
I changed it with 1.9.13 then #JsonIgnore is working fine.
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.1.1</version>
</dependency>
Change to
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.13</version>
</dependency>
And Java Code is :-
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.codehaus.jackson.annotate.JsonIgnore;
#Entity
#Table(name="users")
public class Users implements Serializable{
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String username;
private String firstname;
#JsonIgnore
private String lastname;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
I faced the same issue while working with spring and hibernate. The reason for this was that in entity class I was using org.codehaus.jackson.JsonIgnore and in spring I was using com.fasterxml.jackson.jaxrs.JsonIgnore. Fix to any one library in both layers and problem will disappear.
If #JsonProperty("fieldName") is also present on the field along with #JsonIgnore, the latter is ignored. This could happen, for instance, in generated code.
#Generated("jsonschema2pojo")
public MyGeneratedClass {
// ...
#JsonProperty("label") // This overrides #JsonIgnore
#JsonIgnore(true) // Doesn't work
#Field(name = "label")
private java.lang.String label;
// ...
}
From the documentation of JsonIgnore:
... if only particular accessor is to be ignored ..., this can be done by annotating other not-to-be-ignored accessors with JsonProperty (or its equivalents).
This is my POJO, a simple student class.
#Proxy(lazy = false)
#Entity(name = "Students")
public class Student implements Serializable {
private static final long serialVersionUID = -9182600037012718128L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
#Column
private String name;
private List<Homework> homework; // <-- the problematic line
public Student(){
}
public getId(){return id;}
public setId(long id){this.id = id;}
public getName(){return name;}
public setName(String name){this.name = name;}
public getHomework(){return homework;}
public setHomework(List<Homework> homework){this.homework = homework;}
}
Unfortunately, even though the homework field is not annotated (since I currently do not want to map it to my DB), I get this exception when running my application:
org.hibernate.MappingException: Could not determine type for: java.util.List, at table: Students, for columns: [org.hibernate.mapping.Column(homework)]
This is my hibernate-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<property name="username" value="root" />
<property name="password" value="root" />
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />
<property name="testOnBorrow" value="true" />
<property name="validationQuery" value="select 1" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.hbm2ddl.auto=update
hibernate.show.sql=true
</value>
</property>
<property name="annotatedClasses">
<list>
<value>com.test.entity.Student</value>
</list>
</property>
</bean>
Any help is appreciated!
Thanks!
You can make non mapped field as transient, to make hibernate not try to map it with DB
private transient List<Homework> homework;
or you can annotate it with #javax.persistence.Transient annotation
#Transient
private List<Homework> homework;
One feature of hibernate is, it tries to map all the fields of Entity class with the corresponding columns of the table. So for the variable homework, it searches for the corresponding column with the same name "homework" (case -insensitive) in the mapped table.
See the documentation here, and it says
Every non static non transient property (field or method depending on
the access type) of an entity is considered persistent, unless you
annotate it as #Transient. Not having an annotation for your property
is equivalent to the appropriate #Basic annotation.
I'm using following Frameworks:
Hibernate 4.2.0.Final
Spring 3.2.2.RELEASE
Spring Data Jpa 1.1.0.RELEASE
HSQL 2.2.9
TestNG 6.1.1
I've got 2 Entities:
Entity A:
#Entity
#Table( name = "A" )
public class A {
#Id
#GeneratedValue( strategy = GenerationType.IDENTITY )
private Long id;
#OneToMany( fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "a" )
private Set<B> b = new HashSet<B>();
//... getter and setter
}
and Entity B:
#Entity
#Table( name = "B" )
public class B {
#Id
#GeneratedValue( strategy = GenerationType.IDENTITY )
private Long id;
#ManyToOne
#JoinColumn( name = "a_id" )
private A a;
//... getter and setter
}
And i made Spring Data JPARepositories for them:
#Repository
public interface ARepository
extends JpaRepository<A, Long> {
}
#Repository
public interface BRepository
extends JpaRepository<B, Long> {
}
Then i've wrote a test to see if the mapping works:
#TransactionConfiguration( defaultRollback = true )
#ContextConfiguration
public class ARepositoryTest
extends AbstractTransactionalTestNGSpringContextTests {
#Inject
#Setter
private ARepository aRepository;
#Inject
#Setter
private BRepository bRepository;
#Test( groups = { "integration" } )
public void testSaveWithFeed() {
A a = new A();
aRepository.saveAndFlush( a );
B b = new B();
b.setA( a );
bRepository.saveAndFlush( b );
A findOne = aRepository.findOne( a.getId() );
Assert.assertEquals( 1, findOne.getB().size() );
}
}
But the test fails:
Hibernate: insert into A (id) values (default)
Hibernate: insert into B (id, a_id) values (default, ?)
FAILED: testSaveWithA
java.lang.AssertionError: expected [1] but found [0]
And i don't see why. Is there something missing in the mapping, or is do i have to clear a hibernate cache?
I see that there is no new select-query so hibernate is caching the A object - but shouldn't it update the reference from A to the Bs in it's cache??
As additional info here's my persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
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"
xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="local" transaction-type="RESOURCE_LOCAL" >
</persistence-unit>
</persistence>
My ARepositoryTest-context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="my.package"/>
<import resource="classpath:/SpringBeans.xml"/>
</beans>
and my SpringBeans.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
<context:annotation-config />
<jpa:repositories base-package="my.package.dao" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="local"/>
<property name="dataSource" ref="dataSource" />
<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="org.hibernate.dialect.HSQLDialect"/>
<property name="database" value="HSQL" />
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.show_sql" value="true" />
</map>
</property>
</bean>
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:file:${user.home}/data;shutdown=true" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
</beans>
edit: added version numbers, and xml files
edit: for JB Nizet answer
If i add to Class A
public void addB( B b ) {
this.b.add( b );
}
and adapt the testcase
#Test( groups = { "integration" } )
public void testSaveWithA() {
A a = new A();
aRepository.save( a );
B b = new B();
a.addB( b );
aRepository.saveAndFlush( a );
A findOne = aRepository.findOne( a.getId() );
Assert.assertEquals( findOne.getB().size(), 1 );
}
the test passes
But within the hsql file there is no link between A and B:
INSERT INTO A VALUES(1)
INSERT INTO B VALUES(1,NULL)
So it won't be able to select it correct in an other transaction.
If i expand the new mMethod in A
public void addB( B b ) {
this.b.add( b );
b.setA( this );
}
An exception occures when flushing or committing
java.lang.StackOverflowError
at java.util.HashMap$KeyIterator.<init>(HashMap.java:926)
at java.util.HashMap$KeyIterator.<init>(HashMap.java:926)
at java.util.HashMap.newKeyIterator(HashMap.java:940)
at java.util.HashMap$KeySet.iterator(HashMap.java:974)
at java.util.HashSet.iterator(HashSet.java:170)
at java.util.AbstractSet.hashCode(AbstractSet.java:122)
at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:429)
at my.package.A.hashCode(A.java:17)
at my.package.B.hashCode(B.java:13)
at java.util.AbstractSet.hashCode(AbstractSet.java:126)
at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:429)
at my.package.A.hashCode(A.java:17)
at my.package.B.hashCode(B.java:13)
...
Linenumbers A.java:17 and B.java:13 is where the #Data annotation of Lombok is placed generating my getters and setters.
Resolved by removing the dependency to lombok for hashCode and equals / by using Lomboks #EqualsAndHashCode( exclude = { "b" } )
Your test runs in a single transaction, using a single session. So when you're executing aRepository.findOne(a.getId()), Hibernate returns the A that is already in its first level cache. And since you forgot to add the B to the set of Bs in A, this set is still empty.
It's your responsibility to maintain the coherence of the object graph. If you do b.setA(a), you should also do a.getBs().add(b). The best way is to encapsulate these two operations into a method addB(B b) in A, which adds the B to the set and initializes B.a.
I am having trouble saving data into the database. I can see the insert SQL statement in the logs and also I write the generated ID to the log. The problem I am facing is the data is not being persisted into the database from the session.
I don't have any trouble fetching the data from the database. The user I am using has read/write access to the database and also I don't get any errors.
I am using Spring 4.2.3, Hibernate 5.5.0, MySQL 5 and Primefaces 5.3.
I search all the forums and found nothing that helped me.
Has anybody faced similar issues?
Model
#Entity
#Table(name = "borrower", uniqueConstraints = {
#UniqueConstraint(columnNames = "borrower_id")
})
public class Borrower implements java.io.Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "borrower_id", unique = true, nullable = false)
private Integer borrowerId;
#Column(name = "borrower_code", nullable = true, length = 20)
private String borrowerCode;
#Column(name = "first_name", nullable = false, length = 45)
private String firstName;
#Column(name = "last_name", nullable = true, length = 45)
private String lastName;
#Column(name = "phone", nullable = true, length = 45)
private String phone;
#Column(name = "borrower_status", nullable = false, length = 15)
private String borrowerStatus;
#Column(name = "email_id", nullable = false, length = 200)
private String emailId;
public Borrower() {
}
// Getters and setters
}
DAO
public class BorrowerDAOImpl extends HibernateDaoSupport implements BorrowerDAO {
private final Logger logger = LogManager.getLogger(getClass());
#Autowired
private SessionFactory sessionFactory;
#Override
public boolean saveBorrower(Borrower borrower) {
try {
Session session = sessionFactory.getCurrentSession();
session.persist(borrower);
session.flush();
logger.info("Borrower '{} {}' with Borrower ID '{}' saved successfully.", borrower.getFirstName(), borrower.getLastName(), borrower.getBorrowerId());
session.disconnect();
return true;
}
catch (Exception e) {
logger.error("Unable to add new borrower '{} {}', error: ", borrower.getFirstName(), borrower.getLastName(), e);
return false;
}
}
}
Service
#Service("borrowerBO")
#SessionScoped
#Transactional(readOnly = true)
public class BorrowerBOImpl implements BorrowerBO {
#Autowired
BorrowerDAO borrowerDAO;
#Transactional(readOnly = false)
#Override
public boolean saveBorrower(Borrower borrower) {
return borrowerDAO.saveBorrower(borrower);
}
public BorrowerDAO getBorrowerDAO() {
return borrowerDAO;
}
public void setBorrowerDAO(BorrowerDAO borrowerDAO) {
this.borrowerDAO = borrowerDAO;
}
}
Spring Config
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.movielibrary.bo"/>
<!-- SessionFactory bean -->
<import resource="classpath:HibernateSessionFactory.xml"/>
<!-- Annotate transaction behaviour -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- Transaction manager bean -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- Bean declaration -->
<bean id="borrowerBO" class="com.movielibrary.bo.impl.BorrowerBOImpl">
<property name="borrowerDAO" ref="borrowerDAO" />
</bean>
<bean id="borrowerDAO" class="com.movielibrary.dao.impl.BorrowerDAOImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
Hibernate Configuration
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
<!-- Database configuration bean -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/test_db?relaxAutoCommit=true" />
<property name="username" value="user" />
<property name="password" value="password" />
</bean>
<!-- SessionFactory bean -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- Hibernate annotations property -->
<property name="packagesToScan" value="com.movielibrary.model" />
<property name="annotatedClasses">
<list>
<value>com.movielibrary.model.Borrower</value>
</list>
</property>
</bean>
</beans>