gpt4 book ai didi

java - 从Spring boot JPA连接到oracle表,没有@Entity注释和@Id

转载 作者:行者123 更新时间:2023-12-02 09:18:03 25 4
gpt4 key购买 nike

有没有一种方法可以在不使用@Entity和Id的情况下将pojo映射到oracle表或 View 。我在该 View 中没有标识符列。我知道@Entity需要@Id。有不同的注释吗?我应该使用 JDBC 吗?

最佳答案

请参阅No primary key维基教科书部分。

Sometimes your object or table has no primary key. The best solution in this case is normally to add a generated id to the object and table. If you do not have this option, sometimes there is a column or set of columns in the table that make up a unique value. You can use this unique set of columns as your id in JPA. The JPA Id does not always have to match the database table primary key constraint, nor is a primary key or a unique constraint required.

If your table truly has no unique columns, then use all of the columns as the id. Typically when this occurs the data is read-only, so even if the table allows duplicate rows with the same values, the objects will be the same anyway, so it does not matter that JPA thinks they are the same object. The issue with allowing updates and deletes is that there is no way to uniquely identify the object's row, so all of the matching rows will be updated or deleted.

If your object does not have an id, but its table does, this is fine. Make the object an Embeddable object, embeddable objects do not have ids. You will need a Entity that contains this Embeddable to persist and query it.

关于java - 从Spring boot JPA连接到oracle表,没有@Entity注释和@Id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58866472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com