Latest Added Tutorials
Introduction:
Soft delete is a technique used in database management to mark records as "deleted" without physically removing them from the database. This approach is particularly useful when you want to retain data for historical or audit purposes. In a Spring Boot application using Hibernate as the JPA provider, implementing soft delete functionality can greatly enhance data management. In this article, we will explore how to truly implement soft delete in Spring Boot Hibernate using the CustomInspector class.
Implementing Soft Delete with CustomIn...Continue Reading
Question:
1. What is the reason for setting MappedBy in bidirectional many-to-many relationships?
2. When one table has significant amount of records, while other has a few, which side is better to put mappedBy
Answer:
It's actually a good question, and it helps to understand the concept of an "owning" entity. If you want to prevent both sides (in a bidirectional relationship) from having join tables, a good idea, then you need to have a mappedBy= element on one side.
Whether or not there is a join table is controlled by the mappedBy="name" el...Continue Reading
HsqlDb Connection
1. Download hsqldb.zip file from here
2. Extract zip file
3. Open bin folder in the hsqldb folder
4. Edit runServer.bat file as follows:
cd ..\data
@java -classpath ../lib/hsqldb.jar org.hsqldb.server.Server -database.0 mem:sample -dbname.0 sample
5. Save and run runServer.bat file
6. After running this file you should see the command prompt as follows:
Spring applicationContext-test.xml File
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"...Continue Reading
08-02-2016
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/applicationContext.xml", "classpath:/mvc-dispatcher-servlet.xml",
"classpath:/spring-security.xml"})
@WebAppConfiguration
public class GumrukServiceUtilTest {
@Autowired
WebApplicationContext wac;
@Autowired
private UserService userService;
private MockMvc mockMvc;
@Before
public void setup(){
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();...Continue Reading
Create Password
After downloaded Jasypt CLI Tools, execute following code by using encrypt.sh for linux based OS, or encrypt.bat file for Windows located in bin folder:
encrypt.bat input="secret" password=encryptorpassword algorithm=PBEWithMD5AndTripleDES
Output looks like this: AdK2HjMDfxTABg9ZP3kXSWsKo3t4rSn7
Note: Whenever run above command in command prompt, you will get different password each time because PBEWithMD5AndTripleDES algorithm and many other algorithms use random salt generator. For more information please click
Add Maven Depe...Continue Reading
26-11-2015
First of all, we should set log4j.logger.org.hibernate=TRACE property in log4j.properties file, then enable Apache Lucene indexwriter.infostream property as follows:
<prop key="hibernate.search.default.indexwriter.infostream">true</prop>
Note that we should'nt use this property in runtime because of performance degredation....Continue Reading
If we want to save or update only association table, we shouldn't use cascade in @JoinTable annotation.
UserGroup Table
@Entity
public class UserGroup {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int userGroupId;
@Size(min = 4, max = 20, groups = SizeConstraintGroup.class)
@Column(length = 20)
private String name;
@ManyToMany(fetch = FetchType.LAZY)
@JsonIgnore
@JoinTable(
name = "UserGroup_User",
joinColumns = @JoinColumn(name = "userGroupId", nullable = false, updatable...Continue Reading
In Hibernate ManyToMany relationship, save or saveOrUpdate operations must be as follows:
User Entity
@ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
@JoinTable(
name = "UserRole_User",
joinColumns = @JoinColumn(name = "userId", nullable = false, updatable = false),
inverseJoinColumns = @JoinColumn(name = "userRoleId", nullable = false, updatable = false)
)
private List userRoleList=new ArrayList();
In Line 1, cascade provides that when user is saved, an insert operation will be executed on UserRol...Continue Reading
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.codesenior.telif.local.model"/>
<property name="hibernateProperties">
<props>
Question: How does Hibernate Search (Lucene indexing) work?
I am using Hibernate Search built on top of Lucene indexing. If indexes are created against database table the performance will be good in returning the results.
My question is, once indexes are created, if we query for the results does Hibernate Search fetch results from the original database table using the created indexes? or does it not need to hit the database to fetch the results?
Thanks!
Answer:
Unless you use Projections the indexes are used only to identify the set of primary...Continue Reading
OneToMany Unidirectional Mapping
Question:
In this document (scroll down to the Unidirectional section): http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-mapping-association-collections
It says that a unidirectional one-to-many association with a join table is much preferred to just using a foreign key column in the owned entity. My question is, why is it much preferred?
Answer 1:
Consider the situation where the owned entity type can also be owned by another parent entity type. Do you put foreign key refere...Continue Reading
Using Spring Framework we can see Hibernate statistics such as second level hit count via JVM. JMX is good way to expose parts of your application for monitoring and management.
In order to use Hibernate statistics in JMX, we sould add following dependencies:
org.hibernate
hibernate-jmx
3.5.6-Final
org.hibernate
hibernateContinue Reading
To use EHCache library with Hibernate 4 we should add following dependencies:
org.hibernate
hibernate-ehcache
4.3.8.Final
net.sf.ehcache
ehcache-core
2.6.10
In Spring we can configure hibernate + ehcache as follows:
Problem: org.hibernate.HibernateException: createQuery is not valid without active transaction
Solution: If you are using <prop key="hibernate.current_session_context_class">thread</prop>, remove this. Because:
When using spring and spring managed transactions never mess around with the hibernate.current_session_context_class property UNLESS you are using JTA.
Spring will by default set its own CurrentSessionContext implementation (the SpringSessionContext), however if you set it yourself this will not be the case. Basically breaking proper tr...Continue Reading
For most developers, writing almost the same code for every DAO in an application has become indespensible. To solve this problem and to rescue developers from copying and pasting same codes into every dao classes, we will use DAO pattern.
In this tutorial, I will show you how to create dao pattern in a spring application. The foundation of using a Generic DAO is the CRUD operations that you can perform on each entity.
Generic Dao Implementation
Model Class
Hibernate ORM framework uses model classes for database tables. Therefore we need to creat...Continue Reading