Thera是oracle视图vendor_view和table (供应商表仅包含具有名称id的PK,以便于使用)
create view vendor_view as
select id as vid, 'YES' as active
from vendors;协同实体
@Entity
@Table(name = "vendors")
@SecondaryTable(name = "vendor_view", pkJoinColumns = {@PrimaryKeyJoinColumn(name = "vid", referencedColumnName = "id")})
public class Vendor {
@Id
private Long id;
@Column(table = "vendor_view", name = "vid", insertable = false, updatable = false)
private Long vid;
@Column(table = "vendor_view", name = "active", insertable = false, updatable = false)
private String active;
getter and setter....
}当我试图坚持新的供应商实体,然后面对问题:
org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [insert into vendor_view (vid) values (?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:238)
.....
Caused by: org.hsqldb.HsqlException: INSERT, UPDATE, DELETE or TRUNCATE not permitted for table or view
at org.hsqldb.error.Error.error(Unknown Source)JPA实现是Hibirnate。
问题是为什么Hibirnate为标记为= false、updatable =false的字段生成insert查询?
发布于 2015-12-10 17:33:21
因为hibernate不知道要插入的表是表还是视图,除非它与数据库交互。因此,只有当Java程序与数据库交互时,才能检查运行时异常。
它类似于即使表不存在,它也会进行查询,但是在运行时它会抛出异常。
发布于 2015-12-10 17:57:48
当creating/udpating (相关实体)的责任不在当前实体中时,您就会这样做。你有一个Person和一个Address。您希望将insertable=false、updatable=false与Person实体一起添加到Address实体中,因为创建或更新Person并不是地址实体的责任。情况正好相反。这实际上不是一个技术性的决定,而是一个语义/自然的决定。
https://stackoverflow.com/questions/34208122
复制相似问题