gpt4 book ai didi

java - 如何强制对可连接对象使用给定的表名称?

转载 作者:行者123 更新时间:2023-11-30 05:33:04 25 4
gpt4 key购买 nike

我正在使用 Spring data JPA 并正在设置我的第一个 ManyToMany 关系。我将第一个对象“用户”定义为:

import javax.persistence.*;
import java.util.Set;

@Entity
@Table(name = "SMX0_PAR_USER", schema = "SMX0_INPUT_DAY")
public class User {

@Id
private String userID;
private String username;


@ManyToMany
@JoinTable(
name = "SMX0_PAR_USER_ROLE",
schema = "SMX0_INPUT_DAY",
joinColumns = @JoinColumn(name = "USER_ID", referencedColumnName = "userID"),
inverseJoinColumns = @JoinColumn(name = "ROLE_ID", referencedColumnName = "roleID")
)
private Set<Role> roleSet;


protected User(){

}

public User(String userID, String username){
this.userID = userID;
this.username = username;
}

第二个对象“角色”定义为:

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import java.util.Set;

@Entity
@Table(name = "SMX0_PAR_ROLE", schema = "SMX0_INPUT_DAY")
public class Role {

@Id
private int roleID;
private String rolename;

@ManyToMany(mappedBy = "roleSet")
private Set<User> userSet;


protected Role(){

}

public Role(int roleID, String role){
this.roleID = roleID;
this.rolename = role;
}
  • 两个类中的所有字段都有 getter 和 setter。
  • 在与角色没有多对多关系的情况下获取用户效果非常好。

当尝试访问包含以下 thymeleaf 代码的页面时:

 <tr th:each="user : ${users}">
<td th:text="${user.getUserID()}">user ID</td>
<td th:text="${user.getUsername()}">username</td>
<td><span th:each="role : ${user.getRoleSet()}">
<span th:text="${role.getRolename()}">rolename</span>
</span></td>
</tr>

我遇到异常:

org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates//module/userManagement/index.html]")

Caused by: org.attoparser.ParseException: could not extract ResultSet

Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet

Caused by: java.sql.SQLSyntaxErrorException: ORA-00904: "ROLESET0_"."ROLE_ID": invalid identifier

这里发生了什么,当我显式地将连接表名称设置为其他名称时,为什么会出现对“ROLESET0_”之类的引用?如何强制使用我已经给出的表名?

最佳答案

ROLESET0_ 是 hibernate 在其生成的查询中分配的别名。如果您设置 spring.jpa.show-sql=true 来查看 hibernate 生成的完整查询,可能会有所帮助。您还可以设置 spring.jpa.properties.hibernate.format_sql=true 以使其更具可读性。

关于java - 如何强制对可连接对象使用给定的表名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57164722/

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