Hibernate持久性:未找到列

时间:2015-07-10 21:50:54

标签: java spring hibernate h2

我在找到Column not found错误时,我无法弄清楚出了什么问题,所以我请求帮助。

我需要生成引用TaskUser的{​​{1}}列表。 我正在使用h2数据库。

以下是表格:

Project

这是DAO课程:

CREATE TABLE TASK.APP_USER (
id INT NOT NULL GENERATED ALWAYS AS IDENTITY,
username VARCHAR(25) NOT NULL,
firstName VARCHAR(25) NOT NULL,
lastName VARCHAR(25) NOT NULL,
password VARCHAR(32) NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE TASK.PROJECT (
id INT GENERATED ALWAYS AS IDENTITY,
name VARCHAR(50) NOT NULL,
companyName VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE TASK.PROJECT_TASK (
id INT GENERATED ALWAYS AS IDENTITY,
projectId INT NOT NULL,
userId INT NOT NULL,
description VARCHAR(500) NOT NULL,
estimatedDurationHours INT NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (projectId) REFERENCES TASK.PROJECT(id),
FOREIGN KEY (userId) REFERENCES TASK.APP_USER(id),
);

这是User类,我按照相同的原则映射了Project类:

@Repository
public class TaskDaoHibernate implements TaskDao {
public TaskDaoHibernate() {
}

@Autowired
private SessionFactory sessionFactory;

private Session getCurrentSession() {
    return sessionFactory.getCurrentSession();
}

@SuppressWarnings("unchecked")
@Override
public List<User> fetchAllUsers() {
    return getCurrentSession().createQuery("from User").list();
}

@Override
public User fetchUserById(Integer userId) {
    return (User) getCurrentSession().get(User.class, userId);
}

@SuppressWarnings("unchecked")
@Override
public List<Project> fetchAllProjects() {
    return getCurrentSession().createQuery("from Project").list();
}

@Override
public Project fetchProjectById(Integer projectId) {
    return (Project) getCurrentSession().get(Project.class, projectId);
}

@Override
public void saveTask(Task task) {
    getCurrentSession().save(task);
        }

@SuppressWarnings("unchecked")
@Override
public List<Task> fetchAllTasks() {
    return getCurrentSession().createQuery("from Task").list();
}

@Override
public Task fetchTaskById(Integer taskId) {
    return (Task) getCurrentSession().get(Task.class, taskId);
}

@Override
public void editTask(Task task) {
    getCurrentSession().update(task);
        }

public SessionFactory getSessionFactory() {
    return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}


}

最后一课是Task课程:

@Entity
@Table(name="TASK.PROJECT")
public class Project implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Id
@Column(name = "ID")
@GeneratedValue
private Integer id;

@Column(name = "NAME")
private String nazivProjekta;

@Column(name = "COMPANYNAME")
private String nazivTvrtke;

@ManyToOne
@JoinColumn(name="PROJECTID")
private Task task;

//setters, getters and overrided equal and hashcode methods

这是堆栈跟踪的开始:

@Entity
@Table(name="TASK.PROJECT_TASK")
public class Task implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = -4633753821563474175L;

@OneToMany(mappedBy="id", fetch=FetchType.EAGER)
private List<User> user;

@OneToMany(mappedBy="id", fetch=FetchType.EAGER)
private List<Project> project;

@Column(name = "DESCRIPTION ")
private String opisZadatka;

@Column(name = "ESTIMATEDDURATIONHOURS")
private Integer trajanje;

@Id
@Column(name = "ID")
@GeneratedValue
private Integer id;

public Task(){

}

我认为我发布了所有相关信息,但如果需要其他内容,我很乐意添加

2 个答案:

答案 0 :(得分:1)

您的Task实体有一个List<User>,希望您的User拥有@ManyToOne关系。

但是,您的表TASK.PROJECT_TASKTASK.APP_USER表有一个FK。这意味着,任务与用户之间存在@ManyToOne关系,而不是相反。

答案 1 :(得分:1)

您将表格描述作为事实的来源是不正确的。

  1. 从项目到任务的关系应该是OneToMany而不是ManyToOne。
  2. 同样,从任务到项目的关系应该是ManyToOne而不是OneToMany
  3. 任务和用户之间的关系应该是ManyToOne而不是OneToMany