Java PropertyAccessException:调用setter时发生IllegalArgumentException
我将Spring3与JPA/Hibernate4一起使用。在尝试持久化“产品”实体时,我遇到了一个IllegalArgumentException。 我在Product和Category之间有一个一对多的关系,并且有一个复合键ProductId类,它只有Product和Category的PKs。我使用@IdClass注释,因为这两个PK都是由DB(MySql)自动生成的。 我下面的代码将更好地解释它 我的SQL脚本:Java PropertyAccessException:调用setter时发生IllegalArgumentException,java,mysql,spring,hibernate,jpa,Java,Mysql,Spring,Hibernate,Jpa,我将Spring3与JPA/Hibernate4一起使用。在尝试持久化“产品”实体时,我遇到了一个IllegalArgumentException。 我在Product和Category之间有一个一对多的关系,并且有一个复合键ProductId类,它只有Product和Category的PKs。我使用@IdClass注释,因为这两个PK都是由DB(MySql)自动生成的。 我下面的代码将更好地解释它 我的SQL脚本: CREATE TABLE IF NOT EXISTS `orangeDB`.`
CREATE TABLE IF NOT EXISTS `orangeDB`.`Category` (
`catId` INT NOT NULL AUTO_INCREMENT,
`category` VARCHAR(45) NOT NULL,
`lft` INT NOT NULL,
`rgt` INT NOT NULL,
PRIMARY KEY (`catId`),
UNIQUE INDEX `categoryId_UNIQUE` (`catId` ASC))
ENGINE = InnoDB
AUTO_INCREMENT = 1795327;
CREATE TABLE IF NOT EXISTS `orangeDB`.`Product` (
`prodId` INT(9) NOT NULL AUTO_INCREMENT,
`prefix` CHAR NOT NULL,
`product` VARCHAR(45) NOT NULL,
`quantity` INT(9) NOT NULL,
`price` DOUBLE NOT NULL,
`discount` INT(9) NOT NULL,
`catId` INT NOT NULL,
PRIMARY KEY (`prodId`, `catId`),
INDEX `fk_Product_Category1_idx` (`catId` ASC),
CONSTRAINT `fk_Product_Category1`
FOREIGN KEY (`catId`)
REFERENCES `orangeDB`.`Category` (`catId`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
AUTO_INCREMENT = 1143713;
我的模型类:产品、类别和产品ID组合键类
@SuppressWarnings("serial")
@Entity
@Table(name = "Category", catalog = "orangeDB")
public class Category implements java.io.Serializable {
private Integer catId;
private String category;
private int lft;
private int rgt;
private Set<Product> products = new HashSet<Product>(0);
public Category() {
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "catId", unique = true, nullable = false)
public Integer getCatId() {
return this.catId;
}
public void setCatId(Integer catId) {
this.catId = catId;
}
@Column(name = "category", nullable = false, length = 45)
public String getCategory() {
return this.category;
}
public void setCategory(String category) {
this.category = category;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "category")
public Set<Product> getProducts() {
return this.products;
}
public void setProducts(Set<Product> products) {
this.products = products;
}
public void addProduct(Product product) {
this.products.add(product);
if (product.getCategory() != this) {
product.setCategory(this);
}
}
// other getters and setters
}
@SuppressWarnings("serial")
@Entity
@IdClass(ProductId.class)
@Table(name = "Product", catalog = "orangeDB")
public class Product implements java.io.Serializable {
private Integer prodId;
private Category category;
private char prefix = 'P';
private String product;
private int quantity;
private double price;
private int discount;
public Product() {
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "prodId", nullable = false)
public Integer getProdId() {
return this.prodId;
}
public void setProdId(Integer prodId) {
this.prodId = prodId;
}
@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "catId", referencedColumnName = "catId", nullable = false, insertable = false, updatable = false)
public Category getCategory() {
return this.category;
}
public void setCategory(Category category) {
this.category = category;
}
@Column(name = "product", nullable = false, length = 45)
public String getProduct() {
return this.product;
}
public void setProduct(String product) {
this.product = product;
}
// other getters and setters
}
@SuppressWarnings("serial")
public class ProductId implements java.io.Serializable {
private Integer prodId;
private Integer category;
public ProductId() {
}
public ProductId(Integer prodId, Integer category) {
this.prodId = prodId;
this.category = category;
}
public Integer getProdId() {
return this.prodId;
}
public void setProdId(Integer prodId) {
this.prodId = prodId;
}
public Integer getCategory() {
return this.category;
}
public void setCategory(Integer category) {
this.category = category;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof ProductId))
return false;
ProductId castOther = (ProductId) other;
return (this.getProdId() == castOther.getProdId())
&& (this.getCategory() == castOther.getCategory());
}
public int hashCode() {
int result = 17;
result = 37 * result + this.getProdId();
result = 37 * result + this.getCategory();
return result;
}
}
谁能帮我把这件事做好吗?我参考了一些博客和Java持久性wikibooks,映射似乎很好。我不明白为什么会发生这种异常
编辑
哦,我忘了添加堆栈跟踪。给你
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of model.ProductId.prodId
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:965)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of model.ProductId.prodId
org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387)
org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1310)
org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1316)
org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:881)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:241)
$Proxy32.persist(Unknown Source)
service.ProductServiceImpl.save(ProductServiceImpl.java:42)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
$Proxy35.save(Unknown Source)
controller.Admincontroller.addProduct(Admincontroller.java:276)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:685)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:919)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:851)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of model.ProductId.prodId
org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:119)
org.hibernate.mapping.Component$ValueGenerationPlan.execute(Component.java:423)
org.hibernate.id.CompositeNestedGeneratedValueGenerator.generate(CompositeNestedGeneratedValueGenerator.java:121)
org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117)
org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:78)
org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:208)
org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:151)
org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:78)
org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:853)
org.hibernate.internal.SessionImpl.persist(SessionImpl.java:827)
org.hibernate.internal.SessionImpl.persist(SessionImpl.java:831)
org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:875)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:241)
$Proxy32.persist(Unknown Source)
service.ProductServiceImpl.save(ProductServiceImpl.java:42)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
$Proxy35.save(Unknown Source)
controller.Admincontroller.addProduct(Admincontroller.java:276)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:685)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:919)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:851)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
java.lang.IllegalArgumentException: argument type mismatch
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:65)
org.hibernate.mapping.Component$ValueGenerationPlan.execute(Component.java:423)
org.hibernate.id.CompositeNestedGeneratedValueGenerator.generate(CompositeNestedGeneratedValueGenerator.java:121)
org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117)
org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:78)
org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:208)
org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:151)
org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:78)
org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:853)
org.hibernate.internal.SessionImpl.persist(SessionImpl.java:827)
org.hibernate.internal.SessionImpl.persist(SessionImpl.java:831)
org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:875)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:241)
$Proxy32.persist(Unknown Source)
service.ProductServiceImpl.save(ProductServiceImpl.java:42)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
$Proxy35.save(Unknown Source)
controller.Admincontroller.addProduct(Admincontroller.java:276)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:685)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:919)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:851)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
编辑2:从ProductId类中删除了@IdClass注释。我认为在ProductId类中不需要@IdClass。只有在使用此复合id的实体中才需要它。此特定错误是由于您尝试使用integer,但必须使用ProductId类。这样试试
@SuppressWarnings("serial")
@Entity
@IdClass(ProductId.class)
@Table(name = "Product", catalog = "orangeDB")
public class Product implements java.io.Serializable {
private Integer prodId;
private Category category;
private char prefix = 'P';
private String product;
private int quantity;
private double price;
private int discount;
@Id
@Column(name = "prodId", nullable = false)
public Integer getProdId() {
return this.prodId;
}
public void setProdIt(Integer prodId) {
this.prodId = prodId;
}
@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "catId", referencedColumnName = "catId", nullable = false, insertable = false, updatable = false)
public Category getCategory() {
return this.category;
}
public setProductId(ProductId productId){
this.prodId = productId.getProdId();
this.category = productId.getCategory();
}
//getter&setters
}
在ProductId类中:
private Integer prodId;
private Category category;
public ProductId(Integer prodId, Category category) {
this.prodId = prodId;
this.category = category;
}
getters&setters
然后将其设置为:
Category category = new Category();
ProductId pId = new ProductId(1, category); //id, category
product.setProductId(pId);
也可以试试这个:
new Product(1, category); // pls add this constructor
您忽略这一点的主要原因可能是Produt中的setter和IdClass ProductId具有相似的名称
如果od使用IdClass,则不需要任何@GeneratedValue注释。如果要使用
@IdClass
,则ProductId
类和Product
实体中的属性名称和类型必须匹配
在Product.java
类中,您声明了:
prodId
asInteger
和category
ascategory
但是在ProductId.java
类中,您声明:
prodId
asInteger
和category
asInteger
另外,您不应该在ProductId
类中有@IdClass
声明,您必须删除该声明
尝试以下更改:
Product.java
@Entity
@IdClass(ProductId.class)
@Table(name = "Product")
public class Product implements java.io.Serializable {
private Integer prodId;
private Category category;
private int categoryId; // Adding new property
@Id
@GeneratedValue
@Column(name = "prodId", nullable = false)
public Integer getProdId() {
return this.prodId;
}
@Id
@Column(name = "catId", insertable = false, updatable = false)
public int getCategoryId() {
return categoryId;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "catId", referencedColumnName = "catId", nullable = false, insertable = false, updatable = false)
public Category getCategory() {
return this.category;
}
// Remove the @IdClass annoatation from here
public class ProductId implements java.io.Serializable {
private Integer prodId;
private Integer categoryId; // The property name should match
public ProductId() {
}
public ProductId(Integer prodId, Integer categoryId) {
this.prodId = prodId;
this.categoryId = categoryId;
}
public Integer getProdId() {
return this.prodId;
}
public void setProdId(Integer prodId) {
this.prodId = prodId;
}
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof ProductId))
return false;
ProductId castOther = (ProductId) other;
return (this.getProdId() == castOther.getProdId())
&& (this.getCategoryId() == castOther.getCategoryId());
}
public int hashCode() {
int result = 17;
result = 37 * result + this.getProdId();
result = 37 * result + this.getCategoryId();
return result;
}
}
//二传手和接球手
}
ProductId.java
@Entity
@IdClass(ProductId.class)
@Table(name = "Product")
public class Product implements java.io.Serializable {
private Integer prodId;
private Category category;
private int categoryId; // Adding new property
@Id
@GeneratedValue
@Column(name = "prodId", nullable = false)
public Integer getProdId() {
return this.prodId;
}
@Id
@Column(name = "catId", insertable = false, updatable = false)
public int getCategoryId() {
return categoryId;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "catId", referencedColumnName = "catId", nullable = false, insertable = false, updatable = false)
public Category getCategory() {
return this.category;
}
// Remove the @IdClass annoatation from here
public class ProductId implements java.io.Serializable {
private Integer prodId;
private Integer categoryId; // The property name should match
public ProductId() {
}
public ProductId(Integer prodId, Integer categoryId) {
this.prodId = prodId;
this.categoryId = categoryId;
}
public Integer getProdId() {
return this.prodId;
}
public void setProdId(Integer prodId) {
this.prodId = prodId;
}
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof ProductId))
return false;
ProductId castOther = (ProductId) other;
return (this.getProdId() == castOther.getProdId())
&& (this.getCategoryId() == castOther.getCategoryId());
}
public int hashCode() {
int result = 17;
result = 37 * result + this.getProdId();
result = 37 * result + this.getCategoryId();
return result;
}
}
此更改还将解决与组合标识符不能为null有关的问题。请添加整个stacktrace。它不起作用。仍然显示相同的异常。我从ProductId类中删除了@IdClass注释。Product类中的setProdId()方法采用整数。因此,如果我传递ProductId的实例,编译器会给出一个错误。所以我尝试将Product中的prodId字段从Integer类型更改为ProductId类型,然后按照您所说的做了,但它给出了相同的异常。我认为现在应该可以工作了。看看编辑过的版本。最重要的一点是,ProductId的字段中应该有Category,因为它是ProductId类的一部分。谢谢您的回复。但我想我不明白你的答案。setProdId()方法接受Integer类型的参数。但是在您的解决方案中,您正在将类型为ProductId的pId传递到setProdId()中。编译器出错。抱歉,我写得太匆忙了。现在,它应该更具体一些。但想法是一样的。在您的IdClass中,类别不能为int,因为它在product中属于category类型,并且被标记为@Id.Uff…我仍然无法从背后获得此异常。我不知道ProductId的这个setter方法有什么问题。我完全按照你说的做了,但还是有同样的例外。有没有办法避免使用复合密钥而只使用一个PK?没有用。但我认为这删除了“复合标识符不能为null”的异常。我通过注释掉将prodId设置为1122的语句进行了检查,它没有显示标识符异常。但是ProductId的setter上仍然出现IllegalArgumentException。@OhHaNi,你能发布完整的错误堆栈跟踪吗?你的问题没有完整的错误消息。我已经编辑了我的问题并添加了完整的堆栈跟踪。你可以问更多你想要的细节。