0

I using hibernate with spring boot and PostgreSQL as database. When I am saving an entity in tablethe saved id is different as compare to return by hibernate.

I created table with following query:

Create table notification_Instances(
notification_id serial primary key,
notification_text text,
target_type text,
target text,
notification_status text,
created_at text,
updated_at text,
retries text,
subject text
)

my table entity class is:

@Entity
@Table(name="notification_instances")
public class NotificationInstance implements Serializable {
    private static final long serialVersionUID = 2825168659954221851L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="notification_id")
    long notificationId;

    @Column(name="notification_text")
    String notificationText;

    @Column(name="target_type")
    String targetType;

    @Column(name="target")
    String target;

    @Column(name="notification_status")
    String status;

    @Column(name="created_at")
    String  createdAt;

    @Column(name="updated_at")
    String updatedAt;

    @Column(name="retries")
    int retries;

    @Column(name="subject")
    String subject;

    //getters and setters excluded
}

Repositry interface

@org.springframework.stereotype.Repository
public interface NotificationInstanceRepo extends JpaRepository<NotificationInstance, Long>
{
    @Query("Select n.status from NotificationInstance n where n.notificationId= :id")
    String getStatusById(@Param("id") long id);

    @Modifying
    @Transactional
    @Query("UPDATE NotificationInstance SET status= :updatedStatus, retries= :totalRetries, updatedAt= :updatedOn WHERE notificationId= :id")
    public void updateStatus(@Param("id") long id, @Param("updatedStatus") String updatedStatus, @Param("totalRetries") int totalRetries, @Param("updatedOn") String updatedOn);
}

and following is the code which saves entity into

NotificationInstance notificationInstance = new NotificationInstance();
notificationInstance.setNotificationText("Test body");
notificationInstance.setTargetType("Some channel");                notificationInstance.setStatus("Queued");
notificationInstance.setTarget("target");
notificationInstance.setTargetType("");
notificationInstance.setCreatedAt(dateFormat.format(date));                      notificationInstance.setSubject("Test subject");
notificationInstance.setRetries(0);
logger.info("Saving the notification to database.");
notificationInstanceRepo.save(notificationInstance);
System.out.println(notificationInstance.getNotificationId());

Now when I seed into db the id is not the same. What is the reason for this?

S-Man
  • 22,521
  • 7
  • 40
  • 63
Rohan Singh Dhaka
  • 173
  • 2
  • 8
  • 33
  • Please tell us more about notificationInstance. Is it a new one and you want to create it, or is it an existing one and you want to update it? What is it's notificationId before invoking save(notificationInstance)? – Lukas Risko Feb 28 '16 at 21:41
  • @LukasRisko I updated the question. And yes I am trying to create a new one. – Rohan Singh Dhaka Feb 28 '16 at 21:50
  • 2
    I think you should read http://stackoverflow.com/questions/8625150/why-to-use-returned-instance-after-save-on-spring-data-jpa-repository. It will explain to you why and when to use returned object of save method rather than the passed one. – Lukas Risko Feb 28 '16 at 22:05
  • @LukasRiskoThanks :) – Rohan Singh Dhaka Feb 28 '16 at 22:34

1 Answers1

0

If you use "serial" as genration type in postgreSQL, it has some limiation should be followed when we work with JPA. Please refer here for the limitation.

NOTE: Identity sequencing requires the insert to occur before the id can be assigned, so it is not assigned on persist like other types of sequencing. You must either call commit() on the current transaction, or call flush() on the EntityManager. It may also be that you did not set your primary key column in your table to be an identity type.

S-Man
  • 22,521
  • 7
  • 40
  • 63
Shaan
  • 588
  • 1
  • 4
  • 15