Answer the question
In order to leave comments, you need to log in
Hibernate many-to-one and one-to-many?
There are two entities, the main one (UserEntity) and the plugged one (TodoEntity)
In theory, you need a request userEntity.getTodo(); Get a list of all entries in the Todo table that are associated with a specific user. Associated as follows - the parent_id field in the todo table must be associated with the id field in the users table, where id is the primary key. Those. one user - a lot of things tied to it.
Tried to implement one-to-many and many-to-one, got a mapping error.
Could not determine type for: java.util.Set, at table: test_users, for columns: [org.hibernate.mapping.Column(todoEntity)]
, Googling, I found that there could be a great many problems, but none of the solutions already found solved the problem. Did on this guide (the first part)
Question two guide . Answer the question
In order to leave comments, you need to log in
For example, you can try this:
Suppose you have a Task class, something similar to:
@Entity
@Table (name = "todo")
public class Task {
.....
@ManyToOne
@JoinColumn(name = "userId")
private User responsibleUser;
.....
}
@Entity
@Table (name = "users")
public class User {
.....
@Id
@Column (name = "userId")
private Integer userId;
@OneToMany (mappedBy = "responsibleUser")
private Set<Task> tasks = new HashMap<Task>();
.....
}
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(unique = true, nullable = false)
private String userName
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
private Set<Todo> todos = new HashSet<Todo>();
}
@Entity
public class Todo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne()
@JoinColumn(name = "user_id")
private User user;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question