我正在构建一个基于hibernate的Web应用程序,我在持久化实体时遇到异常:
这些是涉及的实体:
@Entity
@Table(name="part_validation")
public class PartValidation {
@ManyToOne
@JoinColumn(name = "product_part_number")
private Product product;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "product_category_category_id")
private Product category;
@Column(name = "part_number")
private String partNumber;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "Id")
private int id;
@Column(name = "incompatible")
@Type(type = "org.hibernate.type.NumericBooleanType")
private boolean incompatible;
@Column(name = "required")
@Type(type = "org.hibernate.type.NumericBooleanType")
private boolean required;
@Column(name = "max_quantity")
private int maxQuantity;
@Column(name = "min_quantity")
private int minQuantity;
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Product getCategory() {
return category;
}
public void setCategory(Product category) {
this.category = category;
}
public String getPartNumber() {
return partNumber;
}
public void setPartNumber(String partNumber) {
this.partNumber = partNumber;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public boolean isIncompatible() {
return incompatible;
}
public void setIncompatible(boolean incompatible) {
this.incompatible = incompatible;
}
public boolean isRequired() {
return required;
}
public void setRequired(boolean required) {
this.required = required;
}
public int getMaxQuantity() {
return maxQuantity;
}
public void setMaxQuantity(int maxQuantity) {
this.maxQuantity = maxQuantity;
}
public int getMinQuantity() {
return minQuantity;
}
public void setMinQuantity(int minQuantity) {
this.minQuantity = minQuantity;
}
}

@Entity
@Table(name="PRODUCT")
public class Product {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_category_id")
private Category category;
@Column(name = "Price")
private double price;
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
public String getPartNumber() {
return partNumber;
}
public void setPartNumber(String partNumber) {
this.partNumber = partNumber;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getProductLine() {
return ProductLine;
}
public void setProductLine(String productLine) {
ProductLine = productLine;
}
public String getFormFactor() {
return FormFactor;
}
public void setFormFactor(String formFactor) {
FormFactor = formFactor;
}
public String getWarranty() {
return warranty;
}
public void setWarranty(String warranty) {
this.warranty = warranty;
}
public String getSoftware() {
return software;
}
public void setSoftware(String software) {
this.software = software;
}
public String getProcessor() {
return processor;
}
public void setProcessor(String processor) {
this.processor = processor;
}
public String getMemory() {
return memory;
}
public void setMemory(String memory) {
this.memory = memory;
}
public String getGraphics() {
return graphics;
}
public void setGraphics(String graphics) {
this.graphics = graphics;
}
public String getNetwork() {
return network;
}
public void setNetwork(String network) {
this.network = network;
}
public String getDiskStorage() {
return diskStorage;
}
public void setDiskStorage(String diskStorage) {
this.diskStorage = diskStorage;
}
public String getDiskStorageBackplane() {
return diskStorageBackplane;
}
public void setDiskStorageBackplane(String diskStorageBackplane) {
this.diskStorageBackplane = diskStorageBackplane;
}
public String getOpticalStorage() {
return opticalStorage;
}
public void setOpticalStorage(String opticalStorage) {
this.opticalStorage = opticalStorage;
}
public String getPowerSupply() {
return powerSupply;
}
public void setPowerSupply(String powerSupply) {
this.powerSupply = powerSupply;
}
public String getScreen() {
return screen;
}
public void setScreen(String screen) {
this.screen = screen;
}
public String getWireless() {
return wireless;
}
public void setWireless(String wireless) {
this.wireless = wireless;
}
public String getOtherFeatures() {
return otherFeatures;
}
public void setOtherFeatures(String otherFeatures) {
this.otherFeatures = otherFeatures;
}
public String getIOcontroller() {
return IOcontroller;
}
public void setIOcontroller(String iOcontroller) {
IOcontroller = iOcontroller;
}
public String getManageability() {
return manageability;
}
public void setManageability(String manageability) {
this.manageability = manageability;
}
public String getPrimaryConnectivity() {
return primaryConnectivity;
}
public void setPrimaryConnectivity(String primaryConnectivity) {
this.primaryConnectivity = primaryConnectivity;
}
public String getPowerOverEthernet() {
return powerOverEthernet;
}
public void setPowerOverEthernet(String powerOverEthernet) {
this.powerOverEthernet = powerOverEthernet;
}
public String getNetworking() {
return networking;
}
public void setNetworking(String networking) {
this.networking = networking;
}
public String getDiskStorageController() {
return diskStorageController;
}
public void setDiskStorageController(String diskStorageController) {
this.diskStorageController = diskStorageController;
}
public String getInterfaceCardSlots() {
return interfaceCardSlots;
}
public void setInterfaceCardSlots(String interfaceCardSlots) {
this.interfaceCardSlots = interfaceCardSlots;
}
public String getManagement() {
return management;
}
public void setManagement(String management) {
this.management = management;
}
@Column(name="id")
private int productId;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="Part_Number")
private String partNumber;
@Column(name="Description")
private String description;
@Column(name="Product_Line")
private String ProductLine;
@Column(name="Form_Factor")
private String FormFactor;
@Column(name="Warranty")
private String warranty;
@Column(name="Software")
private String software;
@Column(name="Processor")
private String processor;
@Column(name="Memory")
private String memory;
@Column(name="Graphics")
private String graphics;
@Column(name="Network")
private String network;
@Column(name="Disk_Storage")
private String diskStorage;
@Column(name="Disk_Storage_Backplane")
private String diskStorageBackplane;
@Column(name="Optical_Storage")
private String opticalStorage;
@Column(name="Power_Supply")
private String powerSupply;
@Column(name="Screen")
private String screen;
@Column(name="Wireless")
private String wireless;
@Column(name="Other_Features")
private String otherFeatures;
@Column(name="IO_Controller")
private String IOcontroller;
@Column(name="Manageability")
private String manageability;
@Column(name="Primary_Connectivity")
private String primaryConnectivity;
@Column(name="Power_Over_Ethernet")
private String powerOverEthernet;
@Column(name="Networking")
private String networking;
@Column(name="Disk_Storage_Controller")
private String diskStorageController;
@Column(name="Interface_Card_Slots")
private String interfaceCardSlots;
@Column(name="Management")
private String management;
@Column(name="Quantity")
private String quantity;
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
}

@Entity
@Table(name="CATEGORY")
public class Category{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="category_id")
private Integer categoryId;
@Column(name="language_id")
private char languageId;
@Column(name="category_name")
private String categoryName;
@Column(name="category_date")
private Date creationDate;
@Column(name="update_Date")
private Date updateDate;
@Column(name="category_Image_Url")
private String categoryImageUrl;
@Column(name="category_Thumbnail_Url")
private String categoryThumbnailUrl;
@Column(name="state")
private String state;
@OneToMany(mappedBy="category", cascade=CascadeType.ALL)
private Set<Product> product;
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public char getLanguageId() {
return languageId;
}
public void setLanguageId(char languageId) {
this.languageId = languageId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Date getUpdateDate() {
return updateDate;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
public String getCategoryImageUrl() {
return categoryImageUrl;
}
public void setCategoryImageUrl(String categoryImageUrl) {
this.categoryImageUrl = categoryImageUrl;
}
public String getCategoryThumbnailUrl() {
return categoryThumbnailUrl;
}
public void setCategoryThumbnailUrl(String categoryThumbnailUrl) {
this.categoryThumbnailUrl = categoryThumbnailUrl;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Set<Product> getProduct() {
return product;
}
public void setProduct(Set<Product> product) {
this.product = product;
}
@ManyToMany(cascade={CascadeType.ALL})
@JoinTable(name="SUBCAT_CAT",
joinColumns={@JoinColumn(name="category_id")},
inverseJoinColumns={@JoinColumn(name="subcategory_id")})
private Set<Category> subcategory = new HashSet<Category>();
@ManyToMany(mappedBy="subcategory")
private Set<Category> category = new HashSet<Category>();
}
&#13;
@SuppressWarnings("unchecked")
@Override
public void addPartValidation(ArrayList<PartValidation> partValidationList){
Session session = null;
Transaction tx = null;
try {
session = sessionFactory.openSession();
tx = session.beginTransaction();
//some action
for(PartValidation pv:partValidationList){
session.persist(pv);
}
tx.commit();
}catch (Exception ex) {
ex.printStackTrace();
tx.rollback();
}
}
&#13;
在我尝试持久保存PartValidation实体后,我得到了这个日志:
Hibernate: insert into part_validation (product_category_category_id, incompatible, max_quantity, min_quantity, part_number, product_part_number, required) values (?, ?, ?, ?, ?, ?, ?)
org.hibernate.exception.GenericJDBCException: could not execute statement
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:190)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:490)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:195)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:179)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:214)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:206)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)
at com.epic.ecommerce.core.dao.ProductDAOImpl.addPartValidation(ProductDAOImpl.java:136)
at com.epic.ecommerce.core.service.ProductServiceImpl.addPartValidation(ProductServiceImpl.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy37.addPartValidation(Unknown Source)
at com.epic.ecommerce.controller.cms.RuleController.loadRule(RuleController.java:133)
at com.epic.ecommerce.controller.cms.RuleController.uploadFileHandler(RuleController.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.sql.SQLException: Data truncated for column 'product_category_category_id' at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2648)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2077)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2362)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2280)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2265)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:187)
... 74 more
&#13;
我相信这是因为product_category_category_id字段太大而无法将其保存在数据库中,hibernate实体关系映射是否有问题?谢谢