我目前正在研究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;
}
}
我试图使用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){
///
}
我得到的错误是:
org.postgresql.util.PSQLException:错误:运算符不存在: text = bigint
由于这个hibernate格式化的sql: &#34; tblsecure0_.plug_user = authusers1_.id&#34;
应该比较tblsecure0_.plug_user=authusers1_.username
。
如果我的关联映射不正确,希望有人可以帮助我。
TIA:)
答案 0 :(得分:0)
您正在尝试将@ManyToOne
与另一个表中不是主键的外键相关联。
在这种情况下,您需要添加额外信息才能实现这一目标:
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="plug_user", referencedColumnName="username")
public AuthUsers getAuthUsers() {
return this.authUsers;
}