【问题标题】:Id return by hibernate is not same as id saved in PostgreSQL tablehibernate 返回的 id 与 PostgreSQL 表中保存的 id 不同
【发布时间】:2016-06-11 19:50:26
【问题描述】:

我使用带有 Spring Boot 和 PostgreSQL 作为数据库的休眠。当我在表格中保存实体时,保存的 id 与通过休眠返回的比较不同。

我使用以下查询创建了表:

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
)

我的表实体类是:

@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
}

资源库接口

@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);
}

以下是将实体保存到的代码

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());

现在当我播种到 db 时,id 不一样了。这是什么原因?

【问题讨论】:

  • 请告诉我们更多关于 notificationInstance 的信息。它是一个新的并且您想要创建它,还是它是一个现有的并且您想要更新它?调用 save(notificationInstance) 之前的 notifyId 是什么?
  • @LukasRisko 我更新了这个问题。是的,我正在尝试创建一个新的。
  • 我认为你应该阅读stackoverflow.com/questions/8625150/…。它将向您解释为什么以及何时使用保存方法的返回对象而不是传递的对象。
  • @LukasRisko谢谢 :)

标签: java spring hibernate postgresql spring-mvc


【解决方案1】:

如果您在 postgreSQL 中使用“serial”作为生成类型,则在使用 JPA 时应遵循一些限制。限制请参考here

注意: 标识排序要求插入发生在可以分配 id 之前,因此它不像其他类型的排序一样在持久上分配。您必须在当前事务上调用 commit(),或在 EntityManager 上调用 flush()。也可能是您没有将表中的主键列设置为身份类型。

【讨论】:

    猜你喜欢
    • 2014-05-24
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 2015-11-17
    • 2012-04-08
    相关资源
    最近更新 更多