我有2个实体,如下所示:
@Entity
public class Driver {
@Id
@GeneratedValue
private Integer id;
private String name;
@OneToMany
private List<Car> cars = new ArrayList<Car>();
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Entity
public class Car {
@Id
@GeneratedValue
@Column(name="car_id")
private Integer carId;
@Column(name="car_type")
private String carType;
public Integer getCarId() {
return carId;
}
public void setCarId(Integer carId) {
this.carId = carId;
}
public String getCarType() {
return carType;
}
public void setCarType(String carType) {
this.carType = carType;
}
}
Hibernate配置如下:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- SQL Dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- Data base setting -->
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.username">hibernate</property>
<property name="hibernate.connection.password">hibernate</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping class="org.dxc.hibernate.one.to.many.Driver"/>
<mapping class="org.dxc.hibernate.one.to.many.Car"/>
</session-factory>
</hibernate-configuration>
然后我编写了以下代码来将数据插入DB:
private static void test1()
{
Driver driver = new Driver();
driver.setName("Bitt");
Car car1 = new Car();
car1.setCarType("SUV");
Car car2 = new Car();
car2.setCarType("Jeep");
Session session = getSessionFactory().openSession();
session.beginTransaction();
session.save(driver);
session.save(car1);
session.save(car2);
session.getTransaction().commit();
session.close();
}
但我去Oracle只看到Driver和Car表中的数据,但Driver_Car表是空的,我不知道发生了什么。
eclipse控制台中的输出内容如下所示:
INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
Hibernate: drop table Car cascade constraints
Jun 22, 2017 9:51:12 AM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@40499e4f] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Hibernate: drop table Driver cascade constraints
Hibernate: drop table Driver_Car cascade constraints
Hibernate: drop sequence hibernate_sequence
Hibernate: create sequence hibernate_sequence start with 1 increment by 1
Jun 22, 2017 9:51:12 AM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@6b9ce1bf] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Hibernate: create table Car (car_id number(10,0) not null, car_type varchar2(255 char), primary key (car_id))
Hibernate: create table Driver (id number(10,0) not null, name varchar2(255 char), primary key (id))
Hibernate: create table Driver_Car (Driver_id number(10,0) not null, cars_car_id number(10,0) not null)
Hibernate: alter table Driver_Car add constraint UK_esk5nfu72tp8wnrnvgudvsel5 unique (cars_car_id)
Hibernate: alter table Driver_Car add constraint FKskkihgsvn6a35dqroow0yrx3m foreign key (cars_car_id) references Car
Hibernate: alter table Driver_Car add constraint FK9yujoqc8mcau9bvembcfp8kto foreign key (Driver_id) references Driver
Jun 22, 2017 9:51:12 AM org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources
INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@4fc5e095'
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Driver (name, id) values (?, ?)
Hibernate: insert into Car (car_type, car_id) values (?, ?)
Hibernate: insert into Car (car_type, car_id) values (?, ?)
有人可以帮助我吗?