Java Spring Boot访问MariaDB的视图?

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

Java Spring Boot accessing MariaDB's View?

问题

我正在尝试将我的代码与数据库中的视图连接起来。

例如:

SELECT * FROM Employee_Tardiness;

实际的视图更长且更复杂。

JPA是如何实现调用视图以从中调用API的,还是还有其他连接到该视图的方法?

英文:

I'm trying to connect my code with a view in my database.

For example :
SELECT * FROM Employee_Tardiness;
The actual view is longer and more complicate.

How does JPA implement the view to call api from it or is there any other ways to connect to the view?

答案1

得分: 0

你的JPA实现(很可能是Hibernate)不关心你是将实体连接到表还是视图。
因此,对于读取数据,你不需要做任何特殊处理。
只需将实体映射到视图,就像映射到普通表一样。

对于写入操作,情况可能会变得有点复杂,甚至非常棘手。
重要的问题是:“视图是否可更新?
这基本上意味着你的关系型数据库管理系统是否能够将插入/更新语句从你的视图转换为插入/更新语句,作用于你的表。

如果在理论上不成立,理论上可以在视图上使用INSTEAD OF触发器,但这似乎不受MariaDB支持。

注意:Spring Boot与实体映射无关。所有这些都由你的JPA实现完成。即使Spring Data JPA只是让使用JPA更加舒适,映射仍由JPA完成。

英文:

Your JPA implementation (most likely Hibernate) does not care if you are connecting an entity to a table or a view.
Therefore for reading data you have to do nothing special.
Just map an entity to the view as you would to a normal table.

For writing things might get a little or really tricky.
The important question is: "Is the view updatable?"
This basically means is it possible for your RDBMS to translate an insert/update statement to your view into a insert/update statement to your tables.

If that is not the case in theory one could use an INSTEAD OF trigger on the view but this does not seem to be supported by MariaDb.

Note: Spring Boot has nothing to do with the mapping of entities. It is all done by your JPA implementation. Even Spring Data JPA just makes using JPA a little more comfortable. The mapping is still done by JPA.

huangapple
  • 本文由 发表于 2020年5月4日 10:42:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/61584366.html
匿名

发表评论

匿名网友

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

确定