hibernate一对多保存到数据库

时间:2015-06-24 15:04:46

标签: java hibernate

我目前正在使用HibernateJava,我试图在我们的数据库中保留对象。我们有一个Group类和一个ToDoList类。 Group与此ERD中显示的ToDoList具有一对多的关系。

Group类的相关代码:

@Entity
@Table(name = "Group", catalog = "db")
public class Group implements java.io.Serializable{
    private int id;
    private Set<ToDoList> allToDoLists;

    public Group(){
        allToDoLists = new HashSet<ToDoList>(0);
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "Group_id", unique = true, nullable = false)
    public int getId(){
        return this.id;
    }

    public void setId(int id){
        this.id = id;
    }
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "group")
    public Set<ToDoList> getAllToDoLists() {
        return this.allToDoLists;
    }

    public void addTodoList(ToDoList t){
        this.allToDoLists.add(t);
        t.setGroup(this);
    }

    public void setAllToDoLists(Set<ToDoList> allToDoLists) {
        this.allToDoLists = allToDoLists;
    }
}

ToDoList类的相关代码:

@Entity
@Table (name = "Todo_List", catalog = "db")
public class ToDoList {
    private int id;
    private Group group;

    public ToDoList(){}

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "Todo_List_id", unique = true, nullable = false)
    public int getId(){
        return this.id;
    }

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "Group", nullable = false)
    public Group getGroup() {
        return this.group;
    }
    public void setGroup(Group group) {
        this.group = group;
    }
}

出现问题的代码:

ToDoList toDoList = new ToDoList("TodoList1","Iets","24-06-2015","24-06-2015");
Group group = new Group("Groep 1","10-11-2011","10-11-2011");
GroupDao groupDao = new GroupDaoImpl();
groupDao.store(group);
toDoList.setGroup(group);
toDoListDao.store(toDoList);

'groupDao'和'toDoListDao'中存储的代码基本相同:

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(toDoList);
session.getTransaction().commit();

问题 当我尝试存储ToDoList时,我收到以下错误:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Group, Name) values ('2015-06-24', '2015-06-24', 'Iets', 23, 'TodoList1')' at line 1

所需 如果我可以存储Group并且ToDoList也可以保存,那么它将是最好的。 鉴于我们即将截止日期,如果我可以自己存储GroupToDoList

,那也就足够了

1 个答案:

答案 0 :(得分:4)

“Group”是一个SQL关键字(“group by”)。因此,它不是表名的最佳选择。考虑到Hibernate正在生成DBMS不接受的SQL,这看起来会导致冲突。

如果您无法更改表名,则可以通过引用实体中的名称来解决该冲突,如下所示:

@Table(name = "`Group`", catalog = "db")

@Table(name = "\"Group\"", catalog = "db")

编辑:

正如 ug _ 告诉我的那样,您还可以在persistence.xml文件中设置以下hibernate属性,默认情况下将其设置为所有标识符:

<property name="hibernate.globally_quoted_identifiers" value="true" />

我自己从未使用过,但Javadocs确认这确实是一个支持的属性:

https://docs.jboss.org/hibernate/orm/4.2/javadocs/org/hibernate/cfg/AvailableSettings.html