我已经建立了以下Company
和product
个实体之间的OneToMany关系。当我尝试保存company
实例时,会抛出错误column company cannot be null
。 company
列有一个约束,它不能为空。但为什么它为空呢?我错过了什么?
Product.java
@Entity
@Table(name="product")
public class Product {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="name")
private String name;
@ManyToOne
@JoinColumn(name="company")
private Company company;
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Company.java
@Entity
@Table(name="manufacturer")
public class Company {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="name")
private String name;
@OneToMany(cascade=CascadeType.ALL, mappedBy="company")
private List<Product> products;
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
OnlineStore.java
session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Product shoes = new Product();
shoes.setName("Shoes");
Product tshirt = new Product();
tshirt.setName("T-shirt");
Product gloves = new Product();
gloves.setName("Gloves");
List<Product> products = new ArrayList<>();
products.add(shoes);
products.add(tshirt);
products.add(gloves);
Company addidas = new Company();
addidas.setName("Adiddas");
addidas.setProducts(products);
session.save(addidas);
tx.commit();
错误:专栏&#39;公司&#39;不能为空 线程&#34; main&#34;中的例外情况org.hibernate.exception.ConstraintViolationException:无法执行语句 在org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:59) 在org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42) 在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207) 在org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:57) at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42) 在org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2840) 在org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3411) 在org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81) 在org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:619) 在org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:273) 在org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:254) 在org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:299) 在org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:318) 在org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:275) 在org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:182) at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:113) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192) 在org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177) 在org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:97) 在org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73) 在org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:644) 在org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:636) 在org.hibernate.engine.spi.CascadingActions $ 5.cascade(CascadingActions.java:219) 在org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:391) 在org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:316) 在org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:155) 在org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:424) 在org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:356) 在org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:319) 在org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:155) 在org.hibernate.engine.internal.Cascade.cascade(Cascade.java:104) 在org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:445) at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:281) 在org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:182) at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:113) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192) 在org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38) 在org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177) 在org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32) 在org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73) 在org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:675) 在org.hibernate.internal.SessionImpl.save(SessionImpl.java:667) 在org.hibernate.internal.SessionImpl.save(SessionImpl.java:662) 在com.onetomany.model.OnlineStore.main(OnlineStore.java:48) 引起:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:Column&#39; company&#39;不能为空 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) 在com.mysql.jdbc.Util.handleNewInstance(Util.java:425) 在com.mysql.jdbc.Util.getInstance(Util.java:408) 在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935) 在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3970) 在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906) 在com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524) 在com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2677) 在com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549) 在com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861) 在com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073) at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009) 在com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5098) 在com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204) ......还有41个