Hibernate/JPA – 仅在表存在时执行操作

huangapple go评论89阅读模式
英文:

Hibernate/JPA - do operations on table only if exists

问题

我正在寻找一个答案,看看在Hibernate中是否可能实现。

我试图实现的目标是,如果数据库中存在特定的表格,那么应用程序应该对其进行所有常规操作(在代码中存在 - 查找,保存等)。

否则,只需忽略该表格(@Repository)和@Entity类中的字段,并跳过所有相关的代码。

我对忽略特定不存在的表格字段以及该字段是否用@Column注释有相同的问题,如果它在表格中不存在,是否可以忽略该字段?

我想使用JPA的save方法,但如果需要的话,可以忽略该字段。

英文:

I am looking for an answer if it is possible or not in hibernate.

What I am trying to achieve is that if a particular table exists in the DB then the application should do all the regular operations with it (which exists in the code - find,save.. etc.).

Else just ignore the table (@Repository) and the fields in the @Entity class, and will skip all the related code.

I have the same question regarding ignoring particular non-existing field of an existing table and the field is annotated with @Column, is it possible to ignore the field if it does not exist in table?

I want to use save method of JPA but which can ignore that field if needed.

答案1

得分: 2

这是不可能的,因为使用Hibernate时,你将一个类映射到表,而表和所有映射的列必须存在。

你正在尝试做的是动态SQL,在这种情况下,你需要读取数据库字典来检查存在什么,并在运行时生成代码。

英文:

That's impossible because with Hibernate you map a Class to table and the table and all the mapped columns MUST exist.

What you are trying to do is dynamic SQL and there you would need to read the database dictonary to check what exsits and generate the code during runtime.

huangapple
  • 本文由 发表于 2020年8月4日 23:24:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63250104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定