在hibernate中插入多个对象作为实例变量

时间:2014-07-05 06:42:41

标签: java oracle hibernate

请帮助我理解为什么多个对象作为实例变量插入不会被唤醒。

Professor.java

@Entity
public class Professor {
@Id
private long id;
private String name;
@Embedded
private Courses daycourse;
@Embedded
@AttributeOverrides({
        @AttributeOverride(name="code",column=@Column(name="eve_code")),
        @AttributeOverride(name="desc",column=@Column(name="eve_desc"))})
private Courses evecourse;
// gettter and setters
}

Courses.java

@Embeddable
public class Courses {
private String code;
private String desc;
    // gettter and setters
}

Main.java

         Professor prof = new Professor();
         prof.setId(1);
         prof.setName("leo");

         Courses evecourse = new Courses();
         evecourse.setCode("MCA");
         evecourse.setDesc("Desc");

         Courses daycourse = new Courses();
         daycourse.setCode("MSC");
         daycourse.setDesc("Desc");


         prof.setEvecourse(evecourse);
         prof.setDaycourse(daycourse);



         Session session = sessionFactory.openSession();
         session.beginTransaction();
         session.save(prof);            
         session.getTransaction().commit();
         session.close();

输出:

Hibernate: 
    create table Professor (
        id number(19,0) not null,
        code varchar2(255),
        desc varchar2(255),
        eve_code varchar2(255),
        eve_desc varchar2(255),
        name varchar2(255),
        primary key (id)
    )
Hibernate: 
    insert 
    into
        Professor
        (code, desc, eve_code, eve_desc, name, id) 
    values
        (?, ?, ?, ?, ?, ?)
Jul 05, 2014 12:08:51 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1747, SQLState: 42000
Jul 05, 2014 12:08:51 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-01747: invalid user.table.column, table.column, or column specification

Jul 05, 2014 12:08:51 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute statement
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)

2 个答案:

答案 0 :(得分:5)

不要为您的专栏desc命名。它是一个保留的SQL关键字(order by name desc)。

将其命名为description。这不会导致任何SQL问题,它将使您的代码和架构更具可读性。

同样,Courses应命名为Coursedaycourse应命名为dayCourseevecourse应命名为eveningCourse

答案 1 :(得分:3)

ORA-01747错误意味着您尝试使用Oracle中保留关键字的列名。查看Oracle Reserved Words列表。

此处有问题的列名是DESC列。这是一个保留关键字。一个简单的解决方案是覆盖daycourse字段的属性:

@Embedded
@AttributeOverrides({
        @AttributeOverride(name="code",column=@Column(name="day_code")),
        @AttributeOverride(name="desc",column=@Column(name="day_desc"))})
private Courses daycourse;

@Embedded
@AttributeOverrides({
        @AttributeOverride(name="code",column=@Column(name="eve_code")),
        @AttributeOverride(name="desc",column=@Column(name="eve_desc"))})
private Courses evecourse;

但最好的解决方案仍然是避免使用保留关键字。