請幫我理解爲什麼多個對象作爲實例變量插入不是開玩笑的。在休眠中插入多個對象作爲實例變量
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)
感謝您的回答。 – sunleo