我有这张表,其中包含以下数据。
SELECT * FROM test1;
+---------+------------+-----------+---------+-----------+------------+-----------+
| user_id | has_access | image_url | menu_id | menu_name | service_id | user_name |
+---------+------------+-----------+---------+-----------+------------+-----------+
| 11 | 1 | NULL | 1 | a | 1 | aa |
| 11 | 0 | NULL | 2 | b | 1 | aa |
| 11 | 1 | NULL | 3 | c | 1 | aa |
| 22 | 1 | NULL | 1 | a | 1 | bb |
| 22 | 1 | NULL | 2 | b | 1 | bb |
| 22 | 0 | NULL | 3 | c | 1 | bb |
| 33 | 1 | NULL | 1 | a | 1 | cc |
| 33 | 1 | NULL | 2 | b | 1 | cc |
| 33 | 1 | NULL | 3 | c | 1 | cc |
+---------+------------+-----------+---------+-----------+------------+-----------+
9 rows in set (0.001 sec)
实体
@Data
@Entity
@Table(schema = "test1", name = "test1")
public class Test1Table {
@Id
private Integer userId;
@Column
private Integer serviceId;
@Column
private Integer menuId;
@Column
private String menuName;
@Column
private Boolean hasAccess;
@Column
private String userName;
@Column
private String imageUrl;
}
Jpa 文件
public interface JpaDaoTest1Table extends JpaRepository<Test1Table, Integer> {
}
我编写了以下代码来查看可能的输出。
@SpringBootTest
class JpaDaoTest {
@Autowired
private JpaDaoTest1Table jpa;
@Test
public void context() {
assertNotNull(jpa);
var result = jpa.findAll();
System.err.println("----------------------------------");
for(var k: result) {
System.err.println(k);
}
System.err.println("----------------------------------");
}
}
不幸的是我没有得到我期望的结果。 这些是我执行测试代码时得到的结果。
Hibernate:
select
smad1_0.user_id,
smad1_0.has_access,
smad1_0.image_url,
smad1_0.menu_id,
smad1_0.menu_name,
smad1_0.service_id,
smad1_0.user_name
from
test1 smad1_0
----------------------------------
Test1Table (userId=22, serviceId=1, menuId=1, menuName=a, hasAccess=true, userName=bb, imageUrl=null)
Test1Table (userId=33, serviceId=1, menuId=1, menuName=a, hasAccess=true, userName=cc, imageUrl=null)
Test1Table (userId=11, serviceId=1, menuId=1, menuName=a, hasAccess=true, userName=aa, imageUrl=null)
Test1Table (userId=22, serviceId=1, menuId=1, menuName=a, hasAccess=true, userName=bb, imageUrl=null)
Test1Table (userId=33, serviceId=1, menuId=1, menuName=a, hasAccess=true, userName=cc, imageUrl=null)
Test1Table (userId=11, serviceId=1, menuId=1, menuName=a, hasAccess=true, userName=aa, imageUrl=null)
Test1Table (userId=22, serviceId=1, menuId=1, menuName=a, hasAccess=true, userName=bb, imageUrl=null)
Test1Table (userId=33, serviceId=1, menuId=1, menuName=a, hasAccess=true, userName=cc, imageUrl=null)
Test1Table (userId=11, serviceId=1, menuId=1, menuName=a, hasAccess=true, userName=aa, imageUrl=null)
----------------------------------
正如您在列表中看到的,menu_id 和 menu_name 是从简单的选择查询中重复的。我猜这是一个错误。想知道有没有解决办法。
如果您有任何疑问,请告诉我。
你有问题,因为你的@Id userId不是唯一的,这是错误的
为了理解,您需要阅读持久上下文。持久化上下文,它是Map