Spring+Hibernate Unknown column 'users0_.task_id' in 'field list'





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm newbie in Spring and Hibernate. I want create OneToMany relation beetwen two tables User and Task. I think it is stupid mistake in my code but i can't find where. This is my sql code:



CREATE TABLE IF NOT EXISTS `tms`.`task` (
`task_id` INT(11) NOT NULL AUTO_INCREMENT,
`date` DATETIME NULL DEFAULT NULL,
`hour` VARCHAR(45) NULL DEFAULT NULL,
`time` DOUBLE NULL DEFAULT NULL,
`task` VARCHAR(45) NULL DEFAULT NULL,
`description` TEXT NULL DEFAULT NULL,
PRIMARY KEY (`task_id`))
ENGINE = InnoDB
AUTO_INCREMENT = 4
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci

CREATE TABLE IF NOT EXISTS `tms`.`user` (
`user_id` INT(11) NOT NULL,
`name` VARCHAR(45) NULL DEFAULT NULL,
`login` VARCHAR(45) NULL DEFAULT NULL,
`password` VARCHAR(45) NULL DEFAULT NULL,
PRIMARY KEY (`user_id`),
CONSTRAINT `fk_task`
FOREIGN KEY (`user_id`)
REFERENCES `tms`.`task` (`task_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci


User entity:



@Entity
@Table(name="user")
public class User {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="user_id")
private int id;

/* other fields */

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


Task entity:



    @Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="task_id")
private int id;

@Column(name="date")
private Date date;

@Column(name="hour")
private double hour;

@Column(name="description")
private String description;

@Column(name="time")
private double time;

@OneToMany(mappedBy="task")
private List<User> users;


I want get from database TASK object with all USER. Can`t understand the field 'users0_.task_id' exist in the database. I think it is default hibernate naming. Please look at my database because don't have certainty about it.



This is my code which i want fetch data from db:



DAO:



@Override
public Task getTask(int id) {
Session session = sessionFactory.getCurrentSession();
Task task = session.get(Task.class, id);
return task;
}


Service:



@Transactional
@Override
public Task getTask(int id) {
Task task = userDAO.getTask(id);
Hibernate.initialize(task.getUsers());
return task;
}


My controller:



Task task = userService.getTask(1);


and error:




java.sql.SQLSyntaxErrorException: Unknown column 'users0_.task_id' in
'field list'











share|improve this question























  • The message says it all: you need a task_id column in the user table to reference the task that the user is assigned to. You currently use the user_if as a foreign key to the task, which doesn't make sense.

    – JB Nizet
    Nov 16 '18 at 18:49











  • @JBNizet Thanks a lot. It's work!

    – Dawid Pawka
    Nov 16 '18 at 19:24


















0















I'm newbie in Spring and Hibernate. I want create OneToMany relation beetwen two tables User and Task. I think it is stupid mistake in my code but i can't find where. This is my sql code:



CREATE TABLE IF NOT EXISTS `tms`.`task` (
`task_id` INT(11) NOT NULL AUTO_INCREMENT,
`date` DATETIME NULL DEFAULT NULL,
`hour` VARCHAR(45) NULL DEFAULT NULL,
`time` DOUBLE NULL DEFAULT NULL,
`task` VARCHAR(45) NULL DEFAULT NULL,
`description` TEXT NULL DEFAULT NULL,
PRIMARY KEY (`task_id`))
ENGINE = InnoDB
AUTO_INCREMENT = 4
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci

CREATE TABLE IF NOT EXISTS `tms`.`user` (
`user_id` INT(11) NOT NULL,
`name` VARCHAR(45) NULL DEFAULT NULL,
`login` VARCHAR(45) NULL DEFAULT NULL,
`password` VARCHAR(45) NULL DEFAULT NULL,
PRIMARY KEY (`user_id`),
CONSTRAINT `fk_task`
FOREIGN KEY (`user_id`)
REFERENCES `tms`.`task` (`task_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci


User entity:



@Entity
@Table(name="user")
public class User {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="user_id")
private int id;

/* other fields */

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


Task entity:



    @Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="task_id")
private int id;

@Column(name="date")
private Date date;

@Column(name="hour")
private double hour;

@Column(name="description")
private String description;

@Column(name="time")
private double time;

@OneToMany(mappedBy="task")
private List<User> users;


I want get from database TASK object with all USER. Can`t understand the field 'users0_.task_id' exist in the database. I think it is default hibernate naming. Please look at my database because don't have certainty about it.



This is my code which i want fetch data from db:



DAO:



@Override
public Task getTask(int id) {
Session session = sessionFactory.getCurrentSession();
Task task = session.get(Task.class, id);
return task;
}


Service:



@Transactional
@Override
public Task getTask(int id) {
Task task = userDAO.getTask(id);
Hibernate.initialize(task.getUsers());
return task;
}


My controller:



Task task = userService.getTask(1);


and error:




java.sql.SQLSyntaxErrorException: Unknown column 'users0_.task_id' in
'field list'











share|improve this question























  • The message says it all: you need a task_id column in the user table to reference the task that the user is assigned to. You currently use the user_if as a foreign key to the task, which doesn't make sense.

    – JB Nizet
    Nov 16 '18 at 18:49











  • @JBNizet Thanks a lot. It's work!

    – Dawid Pawka
    Nov 16 '18 at 19:24














0












0








0








I'm newbie in Spring and Hibernate. I want create OneToMany relation beetwen two tables User and Task. I think it is stupid mistake in my code but i can't find where. This is my sql code:



CREATE TABLE IF NOT EXISTS `tms`.`task` (
`task_id` INT(11) NOT NULL AUTO_INCREMENT,
`date` DATETIME NULL DEFAULT NULL,
`hour` VARCHAR(45) NULL DEFAULT NULL,
`time` DOUBLE NULL DEFAULT NULL,
`task` VARCHAR(45) NULL DEFAULT NULL,
`description` TEXT NULL DEFAULT NULL,
PRIMARY KEY (`task_id`))
ENGINE = InnoDB
AUTO_INCREMENT = 4
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci

CREATE TABLE IF NOT EXISTS `tms`.`user` (
`user_id` INT(11) NOT NULL,
`name` VARCHAR(45) NULL DEFAULT NULL,
`login` VARCHAR(45) NULL DEFAULT NULL,
`password` VARCHAR(45) NULL DEFAULT NULL,
PRIMARY KEY (`user_id`),
CONSTRAINT `fk_task`
FOREIGN KEY (`user_id`)
REFERENCES `tms`.`task` (`task_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci


User entity:



@Entity
@Table(name="user")
public class User {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="user_id")
private int id;

/* other fields */

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


Task entity:



    @Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="task_id")
private int id;

@Column(name="date")
private Date date;

@Column(name="hour")
private double hour;

@Column(name="description")
private String description;

@Column(name="time")
private double time;

@OneToMany(mappedBy="task")
private List<User> users;


I want get from database TASK object with all USER. Can`t understand the field 'users0_.task_id' exist in the database. I think it is default hibernate naming. Please look at my database because don't have certainty about it.



This is my code which i want fetch data from db:



DAO:



@Override
public Task getTask(int id) {
Session session = sessionFactory.getCurrentSession();
Task task = session.get(Task.class, id);
return task;
}


Service:



@Transactional
@Override
public Task getTask(int id) {
Task task = userDAO.getTask(id);
Hibernate.initialize(task.getUsers());
return task;
}


My controller:



Task task = userService.getTask(1);


and error:




java.sql.SQLSyntaxErrorException: Unknown column 'users0_.task_id' in
'field list'











share|improve this question














I'm newbie in Spring and Hibernate. I want create OneToMany relation beetwen two tables User and Task. I think it is stupid mistake in my code but i can't find where. This is my sql code:



CREATE TABLE IF NOT EXISTS `tms`.`task` (
`task_id` INT(11) NOT NULL AUTO_INCREMENT,
`date` DATETIME NULL DEFAULT NULL,
`hour` VARCHAR(45) NULL DEFAULT NULL,
`time` DOUBLE NULL DEFAULT NULL,
`task` VARCHAR(45) NULL DEFAULT NULL,
`description` TEXT NULL DEFAULT NULL,
PRIMARY KEY (`task_id`))
ENGINE = InnoDB
AUTO_INCREMENT = 4
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci

CREATE TABLE IF NOT EXISTS `tms`.`user` (
`user_id` INT(11) NOT NULL,
`name` VARCHAR(45) NULL DEFAULT NULL,
`login` VARCHAR(45) NULL DEFAULT NULL,
`password` VARCHAR(45) NULL DEFAULT NULL,
PRIMARY KEY (`user_id`),
CONSTRAINT `fk_task`
FOREIGN KEY (`user_id`)
REFERENCES `tms`.`task` (`task_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci


User entity:



@Entity
@Table(name="user")
public class User {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="user_id")
private int id;

/* other fields */

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


Task entity:



    @Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="task_id")
private int id;

@Column(name="date")
private Date date;

@Column(name="hour")
private double hour;

@Column(name="description")
private String description;

@Column(name="time")
private double time;

@OneToMany(mappedBy="task")
private List<User> users;


I want get from database TASK object with all USER. Can`t understand the field 'users0_.task_id' exist in the database. I think it is default hibernate naming. Please look at my database because don't have certainty about it.



This is my code which i want fetch data from db:



DAO:



@Override
public Task getTask(int id) {
Session session = sessionFactory.getCurrentSession();
Task task = session.get(Task.class, id);
return task;
}


Service:



@Transactional
@Override
public Task getTask(int id) {
Task task = userDAO.getTask(id);
Hibernate.initialize(task.getUsers());
return task;
}


My controller:



Task task = userService.getTask(1);


and error:




java.sql.SQLSyntaxErrorException: Unknown column 'users0_.task_id' in
'field list'








mysql spring hibernate






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 18:44









Dawid PawkaDawid Pawka

11




11













  • The message says it all: you need a task_id column in the user table to reference the task that the user is assigned to. You currently use the user_if as a foreign key to the task, which doesn't make sense.

    – JB Nizet
    Nov 16 '18 at 18:49











  • @JBNizet Thanks a lot. It's work!

    – Dawid Pawka
    Nov 16 '18 at 19:24



















  • The message says it all: you need a task_id column in the user table to reference the task that the user is assigned to. You currently use the user_if as a foreign key to the task, which doesn't make sense.

    – JB Nizet
    Nov 16 '18 at 18:49











  • @JBNizet Thanks a lot. It's work!

    – Dawid Pawka
    Nov 16 '18 at 19:24

















The message says it all: you need a task_id column in the user table to reference the task that the user is assigned to. You currently use the user_if as a foreign key to the task, which doesn't make sense.

– JB Nizet
Nov 16 '18 at 18:49





The message says it all: you need a task_id column in the user table to reference the task that the user is assigned to. You currently use the user_if as a foreign key to the task, which doesn't make sense.

– JB Nizet
Nov 16 '18 at 18:49













@JBNizet Thanks a lot. It's work!

– Dawid Pawka
Nov 16 '18 at 19:24





@JBNizet Thanks a lot. It's work!

– Dawid Pawka
Nov 16 '18 at 19:24












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343676%2fspringhibernate-unknown-column-users0-task-id-in-field-list%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343676%2fspringhibernate-unknown-column-users0-task-id-in-field-list%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine