jpa 视图没有主键
时间: 2023-12-06 14:35:35 浏览: 35
是的,JPA支持查询视图或没有主键的表。以下是两种处理这种情况的方法:
1. 使用`@IdClass`注释定义复合主键
```java
@Entity
@IdClass(MyCompositeKey.class)
public class MyView {
@Id
private Long id;
@Id
private String name;
private String description;
// getters and setters
}
public class MyCompositeKey implements Serializable {
private Long id;
private String name;
// constructors, equals, hashCode
}
```
2. 使用`@EmbeddedId`注释定义嵌入式主键
```java
@Entity
public class MyView {
@EmbeddedId
private MyCompositeKey id;
private String description;
// getters and setters
}
@Embeddable
public class MyCompositeKey implements Serializable {
private Long id;
private String name;
// constructors, equals, hashCode
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)