2014-07-05 56 views
2

請幫我理解爲什麼多個對象作爲實例變量插入不是開玩笑的。在休眠中插入多個對象作爲實例變量

Professor.java

@Entity 
public class Professor { 
@Id 
private long id; 
private String name; 
@Embedded 
private Courses daycourse; 
@Embedded 
@AttributeOverrides({ 
     @AttributeOverride(name="code",[email protected](name="eve_code")), 
     @AttributeOverride(name="desc",[email protected](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) 
+0

感謝您的回答。 – sunleo

回答

3

ORA-01747錯誤表示您嘗試使用Oracle中的保留關鍵字列名稱。看看Oracle Reserved Words的列表。

這裏有問題的列名稱是DESC列。這是一個保留關鍵字。一個簡單的解決辦法是也覆蓋屬性爲daycourse領域:

@Embedded 
@AttributeOverrides({ 
     @AttributeOverride(name="code",[email protected](name="day_code")), 
     @AttributeOverride(name="desc",[email protected](name="day_desc"))}) 
private Courses daycourse; 

@Embedded 
@AttributeOverrides({ 
     @AttributeOverride(name="code",[email protected](name="eve_code")), 
     @AttributeOverride(name="desc",[email protected](name="eve_desc"))}) 
private Courses evecourse; 

但最好的解決辦法仍然是避免保留關鍵字。

5

請勿列出您的列名desc。這是一個保留的SQL關鍵字(order by name desc)。

將其命名爲description。這不會導致任何SQL問題,並且會使您的代碼和架構更具可讀性。

同樣,Courses應該命名爲Coursedaycourse應該命名爲dayCourse,並evecourse應該命名爲eveningCourse