我对 Hibernate 和 CrudRepository 有疑问。
我有一个控制器,它返回一组由视图获取的结果。我创建了一个视图,因为我认为这是管理我的数据库实体的最佳方式,它是这样创建的:
CREATE TABLE Characters (
CHARACTER_ID INT ,
CHARACTER_NAME VARCHAR(50) NOT NULL,
CHARACTER_ICON VARCHAR(250) NOT NULL,
gender VARCHAR(10) NOT NULL,
status VARCHAR(15) NOT NULL,
clevel INT NOT NULL,
experience INT NOT NULL,
TOTAL_EXPERIENCE INT NOT NULL,
health INT NOT NULL,
HEALTH_STATUS VARCHAR(20) NOT NULL,
role VARCHAR(20) NOT NULL,
PRIMARY KEY (CHARACTER_ID)
);
CREATE TABLE GUILDROLE (
ROLE_ID INT NOT NULL,
GUILD_ID INT NOT NULL,
NAME VARCHAR(50) NOT NULL,
SALARY INT NOT NULL,
MANAGER BIT NOT NULL,
ROLE_IMG VARCHAR(250),
DESCRIPTION VARCHAR(800),
GUILD_LEVEL INT NOT NULL,
PRIMARY KEY (ROLE_ID,GUILD_ID)
);
CREATE TABLE GUILDMEMBER (
CHARACTER_ID INT NOT NULL,
ROLE_ID INT NOT NULL,
PRIMARY KEY(CHARACTER_ID,ROLE_ID)
);
CREATE VIEW V_GUILDMEMBERS
AS
SELECT
GR.GUILD_ID AS GUILD_ID,
GR.ROLE_ID AS ROLE_ID,
CH.CHARACTER_ID AS CHARACTER_ID,
CH.CHARACTER_NAME AS CHARACTER_NAME,
GR.GUILD_LEVEL AS GUILD_LEVEL,
GR.NAME AS ROLE_NAME,
GR.ROLE_IMG AS ROLE_IMG
FROM GUILDMEMBER GM
JOIN GUILDROLE GR
ON GM.ROLE_ID = GR.ROLE_ID
JOIN CHARACTERS AS CH
ON CH.CHARACTER_ID = GM.CHARACTER_ID;
我已经实现了像这样对 View 建模的类:
import lombok.Data;
import org.hibernate.annotations.Immutable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Immutable
@Data
public class V_GuildMembers {
@Id
@Column(name = "GUILD_ID")
private Integer guildId;
@Column(name = "ROLE_ID")
private Integer roleId;
@Column(name = "CHARACTER_ID")
private Integer characterId;
@Column(name = "CHARACTER_NAME")
private String characterName;
@Column(name = "GUILD_LEVEL")
private Integer guildLevel;
@Column(name = "ROLE_NAME")
private String roleName;
@Column(name = "ROLE_IMG")
private String roleImg;
}
我的 RepositoryService 看起来像这样:
public List<V_GuildMembers> getGuildMembers(Integer guildId){
List<V_GuildMembers> members = new ArrayList<>();
if(guildId != null){
members = guildMemberListRepository.findMembersByGuildId(guildId);
}
return members;
}
最后,我有这样的存储库界面:
import com.novavrbe.vrbe.dto.V_GuildMembers;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
import java.util.Optional;
public interface GuildMemberListRepository extends CrudRepository<V_GuildMembers, Integer>, JpaSpecificationExecutor<V_GuildMembers> {
default List<V_GuildMembers> findMembersByGuildId(Integer guildId) {
return findAll(
(root, query, criteriaBuilder)-> criteriaBuilder.equal(root.get("guildId"), guildId)
);
}
default Optional<V_GuildMembers> getGuildMemberbyID(Integer characterid){
return findOne(((root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.equal(root.get("characterId"),characterid)));
}
}
在不使用 CrudRepository 和在 Sql Manager 或 MySql Workbench 上使用普通 Select 的情况下,一切都可以完美运行。我在同一个 Giuld 中添加了 3 个公会成员,具有不同的名称和角色,但是我从控制器和方法 findMembersByGuildId(guildId) 获得的结果集如下:
"members": [
{
"role_ID": 5,
"character_ID": 1,
"guild_LEVEL": 50,
"guild_ID": 1,
"role_IMG": "",
"character_NAME": "Marzio Paparzio",
"role_NAME": "Legatus"
},
{
"role_ID": 5,
"character_ID": 1,
"guild_LEVEL": 50,
"guild_ID": 1,
"role_IMG": "",
"character_NAME": "Marzio Paparzio",
"role_NAME": "Legatus"
},
{
"role_ID": 5,
"character_ID": 1,
"guild_LEVEL": 50,
"guild_ID": 1,
"role_IMG": "",
"character_NAME": "Marzio Paparzio",
"role_NAME": "Legatus"
}
]
事实上,结果似乎复制了其中一个记录结果的确切时间。我希望有人能给我帮助!
提前致谢