用额外的列来休眠很多很多

时间:2016-12-27 22:29:05

标签: java hibernate many-to-many

我有两个实体“帐户”和“学习”。每个帐户都有很多研究,研究有开始日期和结束日期。所以,我尝试使用额外的列多对多。当我试图获得一个帐户时,我收到了一个错误。

谢谢!

帐户实体

@Entity
@Table(name = "account")
public class Account implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@Column(name = "email")
private String email;
@Column(name = "firstName")
private String firstName;
@Column(name = "lastName")
private String lastName;
@Column(name = "password")
private String password;
@Column(name = "gender")
@Enumerated(EnumType.STRING)
private Gender gender;
@Column(name = "creationDate")
private Date creationDate;
@Column(name = "birthday")
private Date birthday;
@Column(name = "job")
private String job;
@Column(name = "phone")
private String phone;
@Column(name = "profilePhoto")
private byte[] profilePhoto;
@Column(name = "coverPhoto")
private byte[] coverPhoto;
@Column(name = "profilePhotoType")
private String profilePhotoType;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "primaryKey.account",
        cascade = CascadeType.ALL)
private Set<AccountStudy> accountStudies = new HashSet<AccountStudy>();

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public Gender getGender() {
    return gender;
}

public void setGender(Gender gender) {
    this.gender = gender;
}

public Date getCreationDate() {
    return creationDate;
}

public void setCreationDate(Date creationDate) {
    this.creationDate = creationDate;
}

public Date getBirthday() {
    return birthday;
}

public void setBirthday(Date birthday) {
    this.birthday = birthday;
}

public String getJob() {
    return job;
}

public void setJob(String job) {
    this.job = job;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public byte[] getProfilePhoto() {
    return profilePhoto;
}

public void setProfilePhoto(byte[] profilePhoto) {
    this.profilePhoto = profilePhoto;
}

public byte[] getCoverPhoto() {
    return coverPhoto;
}

public void setCoverPhoto(byte[] coverPhoto) {
    this.coverPhoto = coverPhoto;
}

public String getProfilePhotoType() {
    return profilePhotoType;
}

public void setProfilePhotoType(String profilePhotoType) {
    this.profilePhotoType = profilePhotoType;
}

public Set<AccountStudy> getAccountStudies() {
    return accountStudies;
}

public void setAccountStudies(Set<AccountStudy> accountStudies) {
    this.accountStudies = accountStudies;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((email == null) ? 0 : email.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Account other = (Account) obj;
    if (email == null) {
        if (other.email != null)
            return false;
    } else if (!email.equals(other.email))
        return false;
    return true;
}

}

研究实体

@Entity
@Table(name = "study")
public class Study implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Integer id;
@Column(name = "name")
private String name;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "primaryKey.study", cascade = CascadeType.ALL)
private Set<AccountStudy> accountStudies = new HashSet<AccountStudy>();

public Integer getId() {
    return id;
}

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

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Set<AccountStudy> getAccountStudies() {
    return accountStudies;
}

public void setAccountStudies(Set<AccountStudy> accountStudies) {
    this.accountStudies = accountStudies;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Study other = (Study) obj;
    if (id == null) {
        if (other.id != null)
            return false;
    } else if (!id.equals(other.id))
        return false;
    return true;
}

}

AccountStudy实体

@Entity
@Table(name = "account_study")
@AssociationOverrides({
    @AssociationOverride(name = "primaryKey.account", joinColumns =   @JoinColumn(name = "account_email")),
    @AssociationOverride(name = "primaryKey.study", joinColumns = @JoinColumn(name = "study_id")) })
public class AccountStudy implements Serializable {

private static final long serialVersionUID = 1L;

@EmbeddedId
private AccountStudyId primaryKey = new AccountStudyId();
@Column(name = "startDate")
private Date startDate;
@Column(name = "endDate")
private Date endDate;

public AccountStudyId getPrimaryKey() {
    return primaryKey;
}

public void setPrimaryKey(AccountStudyId primaryKey) {
    this.primaryKey = primaryKey;
}

public Date getStartDate() {
    return startDate;
}

public void setStartDate(Date startDate) {
    this.startDate = startDate;
}

public Date getEndDate() {
    return endDate;
}

public void setEndDate(Date endDate) {
    this.endDate = endDate;
}

public Account getAccount() {
    return getPrimaryKey().getAccount();
}

public void setAccount(Account account) {
    getPrimaryKey().setAccount(account);
}

public Study getStudy() {
    return getPrimaryKey().getStudy();
}

public void setStudy(Study study) {
    getPrimaryKey().setStudy(study);
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((primaryKey == null) ? 0 : primaryKey.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    AccountStudy other = (AccountStudy) obj;
    if (primaryKey == null) {
        if (other.primaryKey != null)
            return false;
    } else if (!primaryKey.equals(other.primaryKey))
        return false;
    return true;
}

AccountStudyId类

@Embeddable
public class AccountStudyId implements Serializable {

private static final long serialVersionUID = 1L;

@ManyToOne
private Account account;
@ManyToOne
private Study study;

public Account getAccount() {
    return account;
}

public void setAccount(Account account) {
    this.account = account;
}

public Study getStudy() {
    return study;
}

public void setStudy(Study study) {
    this.study = study;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((account == null) ? 0 : account.hashCode());
    result = prime * result + ((study == null) ? 0 : study.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    AccountStudyId other = (AccountStudyId) obj;
    if (account == null) {
        if (other.account != null)
            return false;
    } else if (!account.equals(other.account))
        return false;
    if (study == null) {
        if (other.study != null)
            return false;
    } else if (!study.equals(other.study))
        return false;
    return true;
}

}

我正在使用登录servlet尝试获取帐户,但收到以下错误:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.myface.core.model.Account.accountStudies, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:163)
at com.myface.commonGWT.shared.Mapper.getAccountBean(Mapper.java:26)
at com.myface.mainGWT.server.LoginServlet.handleRequest(LoginServlet.java:40)
at org.springframework.web.context.support.HttpRequestHandlerServlet.service(HttpRequestHandlerServlet.java:67)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:686)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:501)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:428)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
at org.eclipse.jetty.server.handler.RequestLogHandler.handle(RequestLogHandler.java:68)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
at org.eclipse.jetty.server.Server.handle(Server.java:370)
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:489)
at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:960)
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:1021)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:865)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:668)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
at java.lang.Thread.run(Thread.java:745)

0 个答案:

没有答案