简体   繁体   English

休眠一对多保存到数据库

[英]hibernate one-to-many save to database

Im currently working with Hibernate and Java , im trying to persist objects in our database. 我目前正在使用HibernateJava ,正在尝试将对象持久保存在数据库中。 We have a Group class and a ToDoList class. 我们有一个Group类和一个ToDoList类。 Group has a one-to-many relationship with ToDoList as show in this ERD . Group有一个一对多的关系ToDoList作为显示在此ERD

The relevant code from the Group class: 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;
    }
}

The relevant code from the ToDoList class: 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;
    }
}

The code where the problem occurs: 发生问题的代码:

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);

The code in 'groupDao' and 'toDoListDao' for storing are basicly the same: “ groupDao”和“ toDoListDao”中用于存储的代码基本相同:

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

Problem When I try to store the ToDoList I get the following error: 问题当我尝试存储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

Needed It whould be best if I could store Group and ToDoList whould be save also. 需要如果可以存储GroupToDoList也最好将其保存。 Given our coming deadline it whould also suffice if I can store Group and ToDoList on their own 鉴于我们即将到来的截止日期,如果我可以自己存储GroupToDoList ,谁也足够了

"Group" is an SQL keyword ("group by"). “ Group”是一个SQL关键字(“ group by”)。 As such it is not the best choice of table name. 因此,这不是表名的最佳选择。 It looks like this is causing conflicts given that Hibernate is generating SQL not accepted by the DBMS. 鉴于Hibernate生成了DBMS不接受的SQL,这似乎引起了冲突。

If you can't change the table name, you can probably solve that conflict by quoting the name in your entity, like so: 如果您不能更改表名,则可以通过引用实体中的名称来解决该冲突,如下所示:

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

or 要么

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

EDIT: 编辑:

And as ug_ informs me, you can also set the following hibernate property in your persistence.xml file to do that by default to all identifiers: 就像ug_告诉我的那样,您还可以在persistence.xml文件中设置以下hibernate属性,以默认情况下对所有标识符执行此操作:

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

I have never used that myself, but the Javadocs confirm that this is indeed a supported property: 我自己从未使用过它,但是Javadocs确认这确实是受支持的属性:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM