无法使用复合键保留瞬态对象

时间:2013-03-19 17:28:23

标签: spring hibernate spring-mvc

我有一个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的短暂SessionFactory session保存为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)
...

stacktrace中的项目是:

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

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

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

提前致谢。

编辑 Getters和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);
    }

}

2 个答案:

答案 0 :(得分: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();返回

答案 1 :(得分:0)

为@Embeddable类添加访问器。 get / setId_Order(),get / setId_Item()。