My requirement is to have items that can be in different categories.
Each item must have 1 to Many categories
Each category can have 0 to Many items.
I followed this question and to implement that I did the following, but with following code I cannot add many categories and cannot retrieve the items of each category.
Sample
Item table
item1
item2
Category table
cat1
cat2
cat3
cat4
cat5
Relationship
item1 cat1
item2 cat1
item1 cat2
item1 cat3
item2 cat2
I need to make sure there would not be any duplicate. For example, adding item1 cat1 to the above output that already have item1 cat1 should be failed.
@Entity
public class Item {
@Id
@GeneratedValue
long id;
@Column(unique = true)
String name;
@OneToMany
Set<Category> category = new HashSet<>();
....
@Entity
public class Category {
@Id
@GeneratedValue
long id;
private String name;
...
To retrieve items I have following code which returns following error
ERROR: No value specified for parameter 1
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Criteria criteria = session.createCriteria(Item.class);
crit.add(Restrictions.eq("category", "cat1"));
return crit.list();
Aucun commentaire:
Enregistrer un commentaire