1

我有一个Hibernate(3.3)实体Basket

@Entity
@Table(name = "BASKETS")
@IdClass(BasketId.class)
public class Basket {

    @ManyToOne
    @Id
    @JoinColumn(name = "id_order", referencedColumnName = "id_o")
    private Order id_order;

    @ManyToOne
    @Id
    @JoinColumn(name = "id_item", referencedColumnName = "id_i")
    private Item id_item;

    private int amount;

    public Basket() {}

    // generated getters and setters
}

BasketId原样:

@Embeddable
public class BasketId implements Serializable {

    private int id_order;
    private int id_item;

    public BasketId() {}

    public BasketId(Order o, Item i) {
        id_order = o.getId_o();
        id_item = i.getId_i();
    }

    // public BasketId(int id_order, int id_item) {
    // this.id_item = id_item;
    // this.id_order = id_order;
    // }

    //generated getter and setters
}

问题是当我想Order用 autowired SessionFactory sessionas保存瞬态时session().save(o)

@Entity
@Table(name = "ORDERS")
public class Order {
    @Id
    @GeneratedValue
    @Column(name = "id_o")
    private int id_o;

    @Column(name = "date")
    private Timestamp date;

    @OneToMany(mappedBy = "id_order")
    private Set<Basket> basketItems;

    public Order() {}
    //generated getter and setters
}

我得到了一个愚蠢的例外:

message Request processing failed; nested exception is org.hibernate.PropertyAccessException: could not set a field value by reflection setter of foo.server.hib.BasketId.id_item

description The server encountered an internal error that prevented it from fulfilling this request.

exception

    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.PropertyAccessException: could not set a field value by reflection setter of foo.server.hib.BasketId.id_item
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)

root cause

    org.hibernate.PropertyAccessException: could not set a field value by reflection setter of foo.server.hib.BasketId.id_item
    org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:114)
    org.hibernate.tuple.component.AbstractComponentTuplizer.setPropertyValues(AbstractComponentTuplizer.java:104)
...
root cause

    java.lang.IllegalArgumentException: Can not set int field foo.server.hib.BasketId.id_item to foo.server.hib.Item
    sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146)
    sun.reflect.UnsafeIntegerFieldAccessorImpl.set(UnsafeIntegerFieldAccessorImpl.java:80)
...

堆栈跟踪中的项目是:

public class Item {
    //other fields
    @OneToMany(mappedBy = "id_item")
    private Set<Basket> basketsWithThisItem;

    public Item() {}
    //generated getter and setters
}

如上所示,我尝试使用BasketId(as(Item, Order)(int,int)) 的不同构造函数,并将Object构造函数添加到Basket. 接下来是做一个merge()而不是save()但没有任何帮助。

提前致谢。

编辑Getter 和 Setter 在那里。我使用 create 函数实现为:

@Transactional
public class HibOrderService implements OrderService {
    @Autowired
    private SessionFactory sessionFactory;

    public Session session() {
        return sessionFactory.getCurrentSession();
    }    

    @Override
    public void create(Order o) {
        session().merge(o);
    }

}
4

2 回答 2

0

将访问器添加到您的 @Embeddable 类。获取/setId_Order(),获取/setId_Item()。

于 2013-03-19T17:39:54.820 回答
0

此处存在错误

Can not set int field foo.server.hib.BasketId.id_item to foo.server.hib.Item




 public BasketId(Order o, Item i) {
        id_order = o.getId_o();
        id_item = i.getId_i();
    }

这里的问题是什么 i.getId_i();返回

于 2013-03-19T17:31:51.133 回答