Interview question on same name entity twice in Jpa/Hibernate

Recently when one of my friend went for an interview then interviewer trap him into an interesting question and he discussed with me, so i thought to share his experience .

Question 1 : Interviewer asked him if we can have two entity for a single table .
Answer : He said yes we can have two entity for same table name if we want to, JPA/Hibernate does not stop us from doing that .

Question 2 : Then interviewer asked him, What is DuplicateMappingException : duplicate import.
Answer : Now my friend got stuck because he does not know answer of this question . So he came too me after interview and discuss it with me . Then i told my friend that this exception occurs when we have two same name java class having @entity annotation in different package. Actually what JPA does that it gave a unique name to each entity you define with @entity annotation as defined below .


package com.google.employee;
@Entity
@Table(name="GOOGLE_EMPLOYEE")
public class Employee{
}

package com.ask.employee;
@Entity
@Table(name="ASK_EMPLOYEE")
public class Employee{
}

As we did not provide any name inside @Entity annotation so, JPA gave class name as entity name. JPA does not allow two same name entity and will give exception Entity name must be unique in a persistence unit . So if we want to have two same name classes in different package as our entity then we should provide a name as given below.


package com.google.employee;
@Entity("GoogleEmployee")
@Table(name="GOOGLE_EMPLOYEE")
public class Employee{
}

package com.ask.employee;
@Entity("AskEmployee")
@Table(name="ASK_EMPLOYEE")
public class Employee{
}

and we will use this name in named queries after from clause too.

select askEmployee from AskEmployee askEmployee where askEmployee.employeeName=:emplyeeName

Please Ask Questions or give suggestions through comments in below section .

Other related posts are :
JPA Hibernate Sequence generator generating odd ID value and IntegrityConstraintVolation exception is coming
Top 10 spring interview questions and their answers. Spring basic concepts.
Logback logs are not working. Log4j logs are not working. Steps to make logging work.
MQ connection not closed and giving MQ Error-2009

Please share this posts on facebook,Linkeedin,Google+,Twitter by clicking on below icons.

JPA Hibernate Sequence generator generating odd ID value and IntegrityConstraintVolation exception is coming

When you do not want only hibernate sequence to generate primary key of your all tables then you have three options given by JPA and Hibernate .

  1. Sequence Generator
  2. Identity Column
  3. Table Sequence

Let’s talk about Sequence generator and it’s proper use so that it won’t give you Odd and Negative Numbers as your primary key .

Sequence Generator : To use a specific named sequence object (whether it is generated by schema generation or already exists in the database) you must define a sequence generator using a @SequenceGenerator annotation. In this you provide a unique name as name tag, provide database sequence name in sequenceName tag.You can define sequencegenerator at top of class or field both. To use this sequenceGenerator you need to define @Generatedvalue tag with stratgeyType as sequence and sequenceGenerator name on field for which you want this value to be assigned.
Refer below code.


@Entity
public class Inventory implements Serializable {


@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="InvSeq")
@SequenceGenerator(name="InvSeq",sequenceName="INV_SEQ")
private long id;

We have learn how to use sequence generator but still there is possibility of Odd and Negative Numbers as primary key values.

It will happen when database sequence is already exist in database and you are not defining database through hibernate.
How it is happening: Well JPA have allocationSize tag in Sequencegenerator , value of allocationSize is 50 by default if you have not assigned any value. So when Jpa persist that record and demand sequence value from database, it treat that database sequence has incremented that value by 50 so it will substract 50 from that value and try to persist record with that value.
On the other hand database is incrementing value by it’s increment by syntax given at time of it’s defination . Which is not guaranteed to be increment by 50. So there is a possibility of value mismatch and odd value generation which could give you integrityconstraintviolation exception.
So to avoid this problem you need to define allocationSize equal to increment by of database sequence or as 1.Refer below code.

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="InvSeq")
@SequenceGenerator(name="InvSeq",sequenceName="INV_SEQ", allocationSize=1)
private long id;

If you find this post useful please follow,comment and share it.Please feel free to ask questions if you have any.

Other Related Posts :

Set datasource spring bean properties dynamically if server is not available
Logback logs are not working. Log4j logs are not working. Steps to make logging work.
MQ connection not closed and giving MQ Error-2009