简体   繁体   中英

hibernate one-to-many save to database

Im currently working with Hibernate and Java , im trying to persist objects in our database. We have a Group class and a ToDoList class. Group has a one-to-many relationship with ToDoList as show in this ERD .

The relevant code from the Group class:

@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:

@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:

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:

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. Given our coming deadline it whould also suffice if I can store Group and ToDoList on their own

"Group" is an SQL keyword ("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.

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:

<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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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