UpBox upBox1 = upRepository.saveAndFlush(upBox);
我得到了这个例外:
org.hibernate.HibernateException: The database returned no natively generated identity value
这是我的实体:
@Table(name = "up_box")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class UpBox implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
// other attributes and getters / setters
}
我的触发器;
CREATE OR REPLACE FUNCTION historyFromUpBoxInsertion( ) RETURNS TRIGGER AS $$
BEGIN
INSERT INTO history (
device, payload, created, msg_date,
acked, frame_id, frame_type, link, direction
) VALUES (
new.device, new.payload, new.processed, new.created,
new.acked, new.frame_id, new.frame_type, new.link, 'UP'
);
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER historyFromUpBoxInsertion_trigger BEFORE INSERT ON up_box
FOR EACH ROW
WHEN (new.processed IS NOT NULL)
execute procedure historyFromUpBoxInsertion( );
如果我使用后触发器并在历史表中复制后删除插入的行,一切正常,但我有两个无用的请求,在up_box中插入和删除。是否可以在插入hibernate之前触发它?
谢谢,丹尼斯
答案 0 :(得分:0)
在触发器中不返回null,返回NEW。它会工作。至少在修改后为我工作。