ORA-00904:“ BOOKORDERS”:Hibernate中的无效标识符

时间:2019-05-22 09:49:18

标签: java oracle hibernate oracle11g mapping

我对将订单信息插入oracle数据库有问题。 当我在JUnit类中调用createOrder函数时,它将引发如下所示的错误。

Hibernate: select ORDERS_SEQ.nextval from dual
Hibernate: insert into BOOKORDERS (CUSTOMER_ID, ORDER_DATE, ORDER_STATUS, 
ORDER_TOTAL, PAYMENT_METHOD, RECIPIENT_NAME, RECIPIENT_PHONE, 
SHIPPING_ADDRESS, ORDER_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into DETAILORDER (quantity, subTotal, book, bookorders) 
values (?, ?, ?, ?)
May 22, 2019 12:26:18 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper 
logExceptions
WARN: SQL Error: 904, SQLState: 42000
May 22, 2019 12:26:18 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper 
logExceptions
ERROR: ORA-00904: "BOOKORDERS": invalid identifier

我该如何解决?

我可以在实体包装中与其相关类共享相关代码订单类。

BookOrders类

    @Entity
    @Table(name="BOOKORDERS",catalog = "JSPPROJECTDATABASE")
    public class BookOrders implements Serializable{
    ...

    @Id
    @SequenceGenerator(name="ORDERS_SEQ", sequenceName="ORDERS_SEQ", allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ORDERS_SEQ")
    @Column(name="ORDER_ID", nullable = false)
    private int id;

    @OneToMany(fetch = FetchType.EAGER,mappedBy = "pk.bookorders", cascade=CascadeType.ALL,orphanRemoval = true)
        private Set<DetailOrder> orderDetails = new HashSet<DetailOrder>();
    }

图书班

@Entity
@Table(name="BOOK",catalog = "JSPPROJECTDATABASE"){
        ...

@Id
@SequenceGenerator(name="BOOK_SEQ", sequenceName="BOOK_SEQ", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="BOOK_SEQ")
@Column(name="BOOK_ID", nullable = false)
private int id; 

@OneToMany(fetch = FetchType.EAGER,mappedBy = "pk.book", cascade=CascadeType.ALL)
private Set<DetailOrder> detailOrders = new HashSet<DetailOrder>(); }

详细订单类别

@Entity
@Table(name = "DETAILORDER", catalog = "JSPPROJECTDATABASE")
@AssociationOverrides({
        @AssociationOverride(name = "pk.bookorders", 
            joinColumns = @JoinColumn(name = "ORDER_ID")),
        @AssociationOverride(name = "pk.book", 
            joinColumns = @JoinColumn(name = "BOOK_ID")) })
public class DetailOrder implements Serializable{

private DetailOrderId pk = new DetailOrderId();

    @Id
    @SequenceGenerator(name="DETAIL_ORDER_SEQ", sequenceName="DETAIL_ORDER_SEQ", allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="DETAIL_ORDER_SEQ")
    @Column(name="DETAIL_ORDER_ID", nullable = false)
    private int id;

    @ManyToOne(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinColumn(name = "BOOK_ID")
    private Book book;

    @ManyToOne(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinColumn(name = "ORDER_ID")
    private BookOrders bookorders;

    @Column(name="QUANTITY")
    private int quantity;

    @Column(name="SUBTOTAL")
    private float subTotal;

    @EmbeddedId
    public DetailOrderId getPk() {
        return pk;
    }

    public void setPk(DetailOrderId pk) {
        this.pk = pk;
    }
}

DetailOrderId类

@Embeddable
public class DetailOrderId implements Serializable{

    @ManyToOne(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinColumn(name = "BOOK_ID")
    private Book book;

    @ManyToOne(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinColumn(name = "ORDER_ID")
    private BookOrders bookorders;
}

测试插入代码

@Test
    public void testCreateOrder() {
        System.out.println("testCreateOrder");
        BookOrders bookorders = new BookOrders();

        Customer customer = new Customer();
        customer.setId(42);

        bookorders.setCustomer(customer);
        bookorders.setRecipentName("Recipent Name");
        bookorders.setRecipentPhone("123456789");
        bookorders.setShippingAddress("Address");

        Set<DetailOrder> orderDetails = new HashSet<DetailOrder>();
        DetailOrder detailOrder = new DetailOrder();

        Book book = new Book(24);
        detailOrder.setBook(book);
        detailOrder.setQuantity(1);
        detailOrder.setSubTotal(33.99f);
        detailOrder.setOrders(bookorders);

        orderDetails.add(detailOrder);

        bookorders.setOrderDetails(orderDetails);

        BookOrders savedOrders = ordersDAO.create(bookorders);

        assertNotNull(savedOrders);
        //assertTrue(savedOrders.getId() > 0);


    }

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。

DetailOrder类

public class DetailOrder implements Serializable{

    @EmbeddedId
    @AttributeOverrides({ @AttributeOverride(name = "ORDER_ID", column = @Column(name = "ORDER_ID", nullable = false)),
        @AttributeOverride(name = "BOOK_ID", column = @Column(name = "BOOK_ID", nullable = false))})
    private DetailOrderId pk = new DetailOrderId();

    @ManyToOne(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinColumn(name = "BOOK_ID", insertable = false, updatable = false, nullable = false)
    private Book book;

    @ManyToOne(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinColumn(name = "ORDER_ID", insertable = false, updatable = false, nullable = false)
    private BookOrders bookorders;  }

DetailOrderId类

@Embeddable
public class DetailOrderId implements Serializable{

    @ManyToOne(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinColumn(name = "BOOK_ID", insertable = false, updatable = false, nullable = false)
    private Book book;

    @ManyToOne(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinColumn(name = "ORDER_ID", insertable = false, updatable = false, nullable = false)
    private BookOrders bookorders;

BookOrder类

@OneToMany(fetch = FetchType.EAGER,mappedBy = "pk.bookorders", cascade=CascadeType.ALL)
    private Set<DetailOrder> orderDetails = new HashSet<DetailOrder>();

图书班

@OneToMany(fetch = FetchType.EAGER,mappedBy = "pk.book", cascade=CascadeType.ALL)
    private Set<DetailOrder> detailOrders = new HashSet<DetailOrder>();