Новичок
Профиль
Группа: Участник
Сообщений: 7
Регистрация: 11.9.2012
Репутация: нет Всего: нет
|
Здравствуйте. У меня следующая проблема: необходимо связать в Hiberneit 2 сущности, но не по ID. Первая сущность: | Код | /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package tables;
import java.io.Serializable; import java.util.Date; import javax.persistence.*; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import javax.xml.bind.annotation.XmlRootElement;
/** * * @author root */ @Entity @Table(name = "active_sessions", catalog = "otava", schema = "public") @XmlRootElement public class ActiveSessions implements Serializable { private static final long serialVersionUID = 1L; @Id @Basic(optional = false) @Size(min = 1, max = 16) @Column(name = "login",insertable=false,updatable=false, nullable = false, length = 16) private String login; @Basic(optional = false) @NotNull @Column(name = "bytes_in", nullable = false) private long bytesIn; @Basic(optional = false) @NotNull @Column(name = "bytes_out", nullable = false) private long bytesOut; @Basic(optional = false) @NotNull @Size(min = 1, max = 128) @Column(name = "cid", nullable = false, length = 128) private String cid; @Basic(optional = false) @NotNull @Size(min = 1, max = 128) @Column(name = "did", nullable = false, length = 128) private String did; @Basic(optional = false) @NotNull @Column(name = "duration", nullable = false) private int duration; @Basic(optional = false) @NotNull @Size(min = 1, max = 2147483647) @Column(name = "ip_address", nullable = false, length = 2147483647) private String ipAddress; @Basic(optional = false) @NotNull @Column(name = "last_alive", nullable = false) @Temporal(TemporalType.TIMESTAMP) private Date lastAlive; @Basic(optional = false) @NotNull @Size(min = 1, max = 2147483647) @Column(name = "nas_ip", nullable = false, length = 2147483647) private String nasIp; @Size(max = 32) @Column(name = "port", length = 32) private String port; @Basic(optional = false) @NotNull @Size(min = 1, max = 64) @Column(name = "sid", nullable = false, length = 64) private String sid; @Basic(optional = false) @NotNull @Column(name = "start", nullable = false) @Temporal(TemporalType.TIMESTAMP) private Date start; @Basic(optional = false) @NotNull @Column(name = "stop", nullable = false) @Temporal(TemporalType.TIMESTAMP) private Date stop; @OneToOne() @JoinColumn(name = "login", referencedColumnName = "login") private Logins logins;
public ActiveSessions() { }
public ActiveSessions(String login) { this.login = login; }
public ActiveSessions(String login, long bytesIn, long bytesOut, String cid, String did, int duration, String ipAddress, Date lastAlive, String nasIp, String sid, Date start, Date stop) { this.login = login; this.bytesIn = bytesIn; this.bytesOut = bytesOut; this.cid = cid; this.did = did; this.duration = duration; this.ipAddress = ipAddress; this.lastAlive = lastAlive; this.nasIp = nasIp; this.sid = sid; this.start = start; this.stop = stop; }
public String getLogin() { return login; }
public void setLogin(String login) { this.login = login; }
public long getBytesIn() { return bytesIn; }
public void setBytesIn(long bytesIn) { this.bytesIn = bytesIn; }
public long getBytesOut() { return bytesOut; }
public void setBytesOut(long bytesOut) { this.bytesOut = bytesOut; }
public String getCid() { return cid; }
public void setCid(String cid) { this.cid = cid; }
public String getDid() { return did; }
public void setDid(String did) { this.did = did; }
public int getDuration() { return duration; }
public void setDuration(int duration) { this.duration = duration; }
public String getIpAddress() { return ipAddress; }
public void setIpAddress(String ipAddress) { this.ipAddress = ipAddress; }
public Date getLastAlive() { return lastAlive; }
public void setLastAlive(Date lastAlive) { this.lastAlive = lastAlive; }
public String getNasIp() { return nasIp; }
public void setNasIp(String nasIp) { this.nasIp = nasIp; }
public String getPort() { return port; }
public void setPort(String port) { this.port = port; }
public String getSid() { return sid; }
public void setSid(String sid) { this.sid = sid; }
public Date getStart() { return start; }
public void setStart(Date start) { this.start = start; }
public Date getStop() { return stop; }
public void setStop(Date stop) { this.stop = stop; }
public Logins getLogins() { return logins; }
public void setLogins(Logins logins) { this.logins = logins; }
@Override public int hashCode() { int hash = 0; hash += (login != null ? login.hashCode() : 0); return hash; }
@Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof ActiveSessions)) { return false; } ActiveSessions other = (ActiveSessions) object; if ((this.login == null && other.login != null) || (this.login != null && !this.login.equals(other.login))) { return false; } return true; }
@Override public String toString() { return "tables.ActiveSessions[ login=" + login + " ]"; } }
|
И вторая: | Код | /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package tables;
import java.io.Serializable; import java.math.BigInteger; import java.util.Date; import javax.persistence.*; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import javax.xml.bind.annotation.XmlRootElement;
/** * * @author root */ @Entity @Table(name = "logins", catalog = "otava", schema = "public", uniqueConstraints = { @UniqueConstraint(columnNames = {"login"}), @UniqueConstraint(columnNames = {"login_id"})}) @XmlRootElement public class Logins implements Serializable { private static final long serialVersionUID = 1L; @Basic(optional = true) @NotNull @Size(min = 1, max = 16) @Column(name = "login",updatable=false,insertable=false, nullable = false, length = 16) private String login; @Id @Basic(optional = false) @Column(name = "login_id", nullable = false) private Long loginId; @Column(name = "group_id") private Long groupId; @Column(name = "category_id") private Long categoryId; @Column(name = "technology_id") private Long technologyId;
@Basic(optional = false) @NotNull @Size(min = 1, max = 128) @Column(name = "password", nullable = false, length = 128) private String password; @Basic(optional = false) @NotNull @Column(name = "create_date", nullable = false) @Temporal(TemporalType.TIMESTAMP) private Date createDate; @Basic(optional = false) @NotNull @Column(name = "disabled", nullable = false) private boolean disabled; @Column(name = "lock_date") @Temporal(TemporalType.TIMESTAMP) private Date lockDate; @Column(name = "unlock_date") @Temporal(TemporalType.TIMESTAMP) private Date unlockDate; @Lob @Column(name = "static_ip") private String staticIp; @JoinColumn(name="login",referencedColumnName="login") @OneToOne(cascade = CascadeType.ALL) private ActiveSessions activeSessions; @JoinColumn(name = "user_id", referencedColumnName = "user_id") @ManyToOne private Users userId; @JoinColumn(name = "tarif_id", referencedColumnName = "tarif_id") @ManyToOne private Tarifs tarifId;
public Logins() { }
public Logins(Long loginId) { this.loginId = loginId; }
public Logins(Long loginId, String login, String password, Date createDate, boolean disabled) { this.loginId = loginId; this.login = login; this.password = password; this.createDate = createDate; this.disabled = disabled; }
public Long getLoginId() { return loginId; }
public void setLoginId(Long loginId) { this.loginId = loginId; }
public Long getGroupId() { return groupId; }
public void setGroupId(Long groupId) { this.groupId = groupId; }
public Long getCategoryId() { return categoryId; }
public void setCategoryId(Long categoryId) { this.categoryId = categoryId; }
public Long getTechnologyId() { return technologyId; }
public void setTechnologyId(Long technologyId) { this.technologyId = technologyId; }
public String getLogin() { return login; }
public void setLogin(String login) { this.login = login; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public Date getCreateDate() { return createDate; }
public void setCreateDate(Date createDate) { this.createDate = createDate; }
public boolean getDisabled() { return disabled; }
public void setDisabled(boolean disabled) { this.disabled = disabled; }
public Date getLockDate() { return lockDate; }
public void setLockDate(Date lockDate) { this.lockDate = lockDate; }
public Date getUnlockDate() { return unlockDate; }
public void setUnlockDate(Date unlockDate) { this.unlockDate = unlockDate; }
public Object getStaticIp() { return staticIp; }
public void setStaticIp(String staticIp) { this.staticIp = staticIp; }
public ActiveSessions getActiveSessions() { return activeSessions; }
public void setActiveSessions(ActiveSessions activeSessions) { this.activeSessions = activeSessions; }
public Users getUserId() { return userId; }
public void setUserId(Users userId) { this.userId = userId; }
public Tarifs getTarifId() { return tarifId; }
public void setTarifId(Tarifs tarifId) { this.tarifId = tarifId; }
@Override public int hashCode() { int hash = 0; hash += (loginId != null ? loginId.hashCode() : 0); return hash; }
@Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof Logins)) { return false; } Logins other = (Logins) object; if ((this.loginId == null && other.loginId != null) || (this.loginId != null && !this.loginId.equals(other.loginId))) { return false; } return true; }
@Override public String toString() { return "tables.Logins[ loginId=" + loginId + " ]"; } }
|
Но, если попробовать вывести все элементы первой сущьности, то запрос сгенерированный будет такой: | Код | select this_.login as login1_4_, this_.bytes_in as bytes2_1_4_, this_.bytes_out as bytes3_1_4_, this_.cid as cid1_4_, this_.did as did1_4_, this_.duration as duration1_4_, this_.ip_address as ip7_1_4_, this_.last_alive as last8_1_4_, this_.nas_ip as nas9_1_4_, this_.port as port1_4_, this_.sid as sid1_4_, this_.start as start1_4_, this_.stop as stop1_4_, logins2_.login_id as login1_8_0_, logins2_.login as login8_0_, logins2_.category_id as category2_8_0_, logins2_.create_date as create3_8_0_, logins2_.disabled as disabled8_0_, logins2_.group_id as group5_8_0_, logins2_.lock_date as lock6_8_0_, logins2_.password as password8_0_, logins2_.static_ip as static9_8_0_, logins2_.tarif_id as tarif12_8_0_, logins2_.technology_id as technology10_8_0_, logins2_.unlock_date as unlock11_8_0_, logins2_.user_id as user13_8_0_, activesess3_.login as login1_1_, activesess3_.bytes_in as bytes2_1_1_, activesess3_.bytes_out as bytes3_1_1_, activesess3_.cid as cid1_1_, activesess3_.did as did1_1_, activesess3_.duration as duration1_1_, activesess3_.ip_address as ip7_1_1_, activesess3_.last_alive as last8_1_1_, activesess3_.nas_ip as nas9_1_1_, activesess3_.port as port1_1_, activesess3_.sid as sid1_1_, activesess3_.start as start1_1_, activesess3_.stop as stop1_1_, tarifs4_.tarif_id as tarif1_13_2_, tarifs4_.city_id as city2_13_2_, tarifs4_.disable as disable13_2_, tarifs4_.isg_group_name_id as isg4_13_2_, tarifs4_.lk_visible as lk5_13_2_, tarifs4_.tarif_group_id as tarif6_13_2_, tarifs4_.tarif_name as tarif7_13_2_, tarifs4_.tarif_name_id as tarif8_13_2_, tarifs4_.technology_id as technology9_13_2_, users5_.user_id as user1_0_3_, users5_.category_id as category2_0_3_, users5_.create_date as create3_0_3_, users5_.description as descript4_0_3_, users5_.fio as fio0_3_, users5_.group_id as group6_0_3_, users5_.lock_date as lock7_0_3_, users5_.locked as locked0_3_, users5_.ls as ls0_3_, users5_.ustr as ustr0_3_ from otava.public.active_sessions this_ inner join otava.public.logins logins2_ on this_.login=logins2_.login_id left outer join otava.public.active_sessions activesess3_ on logins2_.login=activesess3_.login left outer join otava.public.tarifs tarifs4_ on logins2_.tarif_id=tarifs4_.tarif_id left outer join otava.public.users users5_ on logins2_.user_id=users5_.user_id
|
И само собой на это inner join otava.public.logins logins2_ on this_.login=logins2_.login_id он грязно выругается. (SEVERE: ERROR: operator does not exist: character varying = bigint) Как можно решить эту проблемму?
|