Postgresql 在外键上进行Hibernate 5映射

Postgresql 在外键上进行Hibernate 5映射,postgresql,hibernate,annotations,Postgresql,Hibernate,Annotations,我目前正在学习hibernate,我有以下postgresql表结构: CREATE TABLE tbl_secure ( plug_id bigserial NOT NULL, plug_name text DEFAULT ''::text, plug_user text DEFAULT ''::text, CONSTRAINT "PK_PLUG_ID" PRIMARY KEY (plug_id), CONSTRAINT tbl_secure_plug_user_fkey

我目前正在学习hibernate,我有以下postgresql表结构:

CREATE TABLE tbl_secure
(
  plug_id bigserial NOT NULL,
  plug_name text DEFAULT ''::text,
  plug_user text DEFAULT ''::text,
  CONSTRAINT "PK_PLUG_ID" PRIMARY KEY (plug_id),
  CONSTRAINT tbl_secure_plug_user_fkey FOREIGN KEY (plug_user)
      REFERENCES auth_users (username) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE
)

CREATE TABLE auth_users
(
  id bigint NOT NULL DEFAULT nextval('users_id_seq'::regclass),
  username character varying(64) NOT NULL,
  passwd character varying(64) NOT NULL,
  pass_expiry date DEFAULT ((now())::date + '1 mon'::interval),
  CONSTRAINT users_pkey PRIMARY KEY (id),
  CONSTRAINT auth_users_username_key UNIQUE (username)
)
和hibernate注释实体:

@Entity
@Table(name="tbl_secure",schema="public")
public class TblSecure  implements java.io.Serializable {

     private Long plugId;
     private AuthUsers authUsers;
     private String plugName;

    public TblSecure() {
    }

    @Id @GeneratedValue(strategy=IDENTITY)
    @Column(name="plug_id", unique=true, nullable=false)
    public Long getPlugId() {
        return this.plugId;
    }

    public void setPlugId(Long plugId) {
        this.plugId = plugId;
    }

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="plug_user")
    public AuthUsers getAuthUsers() {
        return this.authUsers;
    }

    public void setAuthUsers(AuthUsers authUsers) {
        this.authUsers = authUsers;
    }


    @Column(name="plug_name")
    public String getPlugName() {
        return this.plugName;
    }

    public void setPlugName(String plugName) {
        this.plugName = plugName;
    }
}

@Entity
@Table(name="auth_users",schema="public", uniqueConstraints =@UniqueConstraint(columnNames="username"))

public class AuthUsers  implements java.io.Serializable {
     private Long id;
     private String username;
     private String passwd;
     private Set tblSecures = new HashSet(0);

    public AuthUsers() {
    }

    @Id @GeneratedValue(strategy=IDENTITY)

    @Column(name="id", unique=true, nullable=false)
    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }


    @Column(name="username", unique=true, nullable=false, length=64)
    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }


    @Column(name="passwd", nullable=false, length=64)
    public String getPasswd() {
        return this.passwd;
    }

    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

    @OneToMany(fetch=FetchType.LAZY, mappedBy="authUsers")
    public Set<TblSecure> getTblSecures() {
        return this.tblSecures;
    }

    public void setTblSecures(Set<TblSecure> tblSecures) {
        this.tblSecures = tblSecures;
    }
}
@实体
@表(name=“tbl\u secure”,schema=“public”)
公共类TblSecure实现java.io.Serializable{
私有长plugId;
私人授权用户;
私有字符串名;
公共TblSecure(){
}
@Id@GeneratedValue(策略=标识)
@列(name=“plug\u id”,unique=true,nullable=false)
公共长getPlugId(){
返回这个.plugId;
}
public void setPlugId(长plugId){
this.plugId=plugId;
}
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“plug\u user”)
公共AuthUsers getAuthUsers(){
返回此.authUsers;
}
公共void setAuthUsers(AuthUsers AuthUsers){
this.authUsers=authUsers;
}
@列(name=“plug\u name”)
公共字符串getPlugName(){
返回此.plugName;
}
public void setPlugName(字符串plugName){
this.plugName=plugName;
}
}
@实体
@表(name=“auth_users”,schema=“public”,uniqueConstraints=@UniqueConstraint(columnNames=“username”))
公共类AuthUsers实现java.io.Serializable{
私人长id;
私有字符串用户名;
私有字符串passwd;
私有集tblSecures=新哈希集(0);
公共授权用户(){
}
@Id@GeneratedValue(策略=标识)
@列(name=“id”,unique=true,nullable=false)
公共长getId(){
返回此.id;
}
公共无效集合id(长id){
this.id=id;
}
@列(name=“username”,unique=true,nullable=false,length=64)
公共字符串getUsername(){
返回此用户名;
}
public void setUsername(字符串用户名){
this.username=用户名;
}
@列(name=“passwd”,null=false,长度=64)
公共字符串getPasswd(){
返回此.passwd;
}
公共void setPasswd(字符串passwd){
this.passwd=passwd;
}
@OneToMany(fetch=FetchType.LAZY,mappedBy=“authUsers”)
公共集getTblSecures(){
返回此.tblSecures;
}
公共无效设置tblSecures(设置tblSecures){
this.tblSecures=tblSecures;
}
}

我正在尝试使用HQL获取:

Query query = session.createQuery("from TblSecure sec WHERE        sec.uathUser.username=:pUser AND sec.plugName=:pName AND     sec.plugSystem=:pSystem");

query.setParameter("pUser", "user");

query.setParameter("pName", "plugname");

query.setParameter("pSystem", "projectname");

List<TblSecure> result = query.list();

for(TblSecure sec2 : result){
 ///
}
Query Query=session.createQuery(“来自TblSecure sec,其中sec.uatuser.username=:pUser和sec.plugName=:pName和sec.plugSystem=:pSystem”);
setParameter(“pUser”、“user”);
setParameter(“pName”、“plugname”);
query.setParameter(“pSystem”、“projectname”);
列表结果=query.List();
对于(TblSecure sec2:结果){
///
}

我得到的错误是:

org.postgresql.util.PSQLException:错误:运算符不存在: text=bigint

由于此hibernate格式的sql: “tblsecure0.plug\u user=authusers1.id”

它应该比较
tblsecure0.plug\u user=authusers1\uUserName

希望有人能帮助我,如果我有一个不正确的关联映射


TIA:)

您正试图使用
@ManyToOne
来关联另一个表中不是主键的外键

在这种情况下,您需要添加额外的信息以使其正常工作:

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="plug_user", referencedColumnName="username")
public AuthUsers getAuthUsers() {
    return this.authUsers;
}

伟大的很高兴我能帮忙