gpt4 book ai didi

java - hibernate 如何从现有数据库 View 中检索数据?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:18:01 27 4
gpt4 key购买 nike

我刚接触 hibernate 。我的问题是我有一个 Oracle 数据库。我在数据库中有一个 View 。现在我想使用 hibernate 来检索该 View 中的数据。有没有可能的解决方案?

最佳答案

以下代码片段可以解决您的问题,该代码摘自教程:Mapping Hibernate Entities to Views

Database Query 

CREATE OR REPLACE VIEW cameron AS
SELECT last_name AS surname
FROM author
WHERE first_name = 'Cameron';

view entity

@Entity
@NamedNativeQuery(name = "findUniqueCameronsInOrder", query = "select * from cameron order by surname", resultClass = Cameron.class)
public class Cameron implements java.io.Serializable {

private static final long serialVersionUID = 8765016103450361311L;

private String surname;

@Id
@Column(name = "SURNAME", nullable = false, length = 50)
public String getSurname() {
return surname;
}

public void setSurname(final String surname) {
this.surname = surname;
}
}

Hibernate mapping file.

<mapping class="examples.hibernate.spring.query.domain.Cameron" />

finally some test !...

@Test
public void findTheCameronsInTheView() throws Exception {
final List<Cameron> camerons = findUniqueCameronsInOrder();
assertEquals(2, camerons.size());
final Cameron judd = camerons.get(0);
final Cameron mcKenzie = camerons.get(1);
assertEquals("Judd", judd.getSurname());
assertEquals("McKenzie", mcKenzie.getSurname());
}

关于java - hibernate 如何从现有数据库 View 中检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4676162/

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