猿问

我怎样才能强制使用给定的表名来连接?

我正在使用 Spring 数据 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>


这里发生了什么,当我明确地将连接表名称设置为其他名称时,为什么会引用“ROLESET0_”之类的内容?我怎样才能强制使用我已经给出的表名?


慕斯709654
浏览 122回答 2
2回答

绝地无双

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

慕田峪7331174

@Column (name = "YourColumnName")尝试为两个类中的所有字段添加注释。此外,referencedColumnName参数应该是该外键列引用的列的名称。&nbsp; &nbsp; public class User {&nbsp; &nbsp; &nbsp; &nbsp;@Id&nbsp; &nbsp; &nbsp; &nbsp;@Column(name = "YourColumnName")&nbsp; &nbsp; &nbsp; &nbsp;private int roleID;&nbsp; &nbsp; &nbsp; &nbsp;@Column(name = "YourColumnName")&nbsp; &nbsp; &nbsp; &nbsp;private String rolename;&nbsp; &nbsp; &nbsp; &nbsp;@ManyToMany&nbsp; &nbsp; &nbsp; &nbsp; @JoinTable(&nbsp; &nbsp; &nbsp; &nbsp; name = "SMX0_PAR_USER_ROLE",&nbsp; &nbsp; &nbsp; &nbsp; schema = "SMX0_INPUT_DAY",&nbsp; &nbsp; &nbsp; &nbsp; joinColumns = @JoinColumn(name = "USER_ID", referencedColumnName = "YourColumnName"),&nbsp; &nbsp; &nbsp; &nbsp; inverseJoinColumns = @JoinColumn(name = "ROLE_ID", referencedColumnName ="YourColumnName"))&nbsp; &nbsp; &nbsp; &nbsp; .....&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;public class Role {&nbsp; &nbsp; &nbsp; &nbsp;@Id&nbsp; &nbsp; &nbsp; &nbsp;@Column(name = "YourColumnName")&nbsp; &nbsp; &nbsp; &nbsp;private int roleID;&nbsp; &nbsp; &nbsp; &nbsp;@Column(name = "YourColumnName")&nbsp; &nbsp; &nbsp; &nbsp;private String rolename;&nbsp; &nbsp; &nbsp; &nbsp;.......&nbsp; &nbsp; &nbsp; &nbsp;}
随时随地看视频慕课网APP

相关分类

Java
我要回答