【问题标题】:Java EE - One to many bidirectionalJava EE - 一对多双向
【发布时间】:2011-01-05 22:08:54
【问题描述】:

据我了解,单向一对多关系有一个连接表,而双向一对多关系没有。

当我实现单向关系时,我的应用程序可以工作,但我似乎无法让它为双向关系工作。

这些是我正在创建的表

CREATE TABLE CUSTOMER (customerNo INTEGER PRIMARY KEY, joinDate DATE, customerName VARCHAR2(20));

CREATE TABLE BOOKING (bookingNo INTEGER PRIMARY KEY, bookedDate DATE, custNo INTEGER, itemNo NUMBER(10), itemName VARCHAR2(20), quantity NUMBER(5), bookingDate DATE, bookingValue NUMBER(8, 2), constraint booking_fk foreign key (custNo) references customer(customerNo));

我的第一堂课

  public class Booking implementes Serializable{
  private Timestamp bookeddate;
      private Timestamp bookingdate;

  @Id()
   @GeneratedValue(generator"MY_SEQ_GEN")
   @SequenceGenerator(name="MY_SEQ_GEN", sequenceName="MY_SEQUENCE", allocationSize=1)
   @Column(name="bookingNo", nullable=false)
  private Long bookingno;
  private Double bookingvalue;

  @Column(length = 20)
  private String itemname;
  private Long itemno;
  private Long quantity;

  private Customer customer;

  @ManyToOne
  public Customer getCustomer() {
    return customer;
  }

  ...

我的其他班级

  public class Customer implements Serializable {
  @Column(length = 20)
  private String customername;
  @Id()
     @GeneratedValue(generator="THE_SEQ_GEN")
     @SequenceGenerator(name="THE_SEQ_GEN", sequenceName="THE_SEQUENCE", allocationSize=1)
     @Column(name="customerNo", nullable=false)
  private Long customerno;
  private Timestamp joindate;

  @OneToMany(cascade=(CascadeType.ALL), fetch=FetchType.EAGER, mappedBy = "customer")
  private List<Booking> bookings = new ArrayList<Booking>();

  public List<Booking> getBookings() {
      return bookings;
    }

运行此方法后,我的 bean 摔倒了

public void addBooking(Long custno, Long tickno, Long quantity) { 
    Customer cust = (Customer) em.createNamedQuery("findCustomerByPrimaryKey").setParameter("eid", custno).getSingleResult();
    Booking b = new Booking();

        b.setBookeddate(new Timestamp(System.currentTimeMillis()));

        b.setCustomer(cust);

        b.setTicket((Ticket) em.createNamedQuery("findTicketByPrimaryKey").setParameter("eid", tickno).getSingleResult(), quantity);

        cust.addBooking(b);

        //persistBooking(b);
  }

这是我的错误信息。

javax.ejb.EJBException: BEA1-001D72BA69DC9E472B1E: Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.2.v20100323-r6872): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: ORA-00904: "CUSTOMER_CUSTOMERNO": invalid identifier

Error Code: 904
Call: INSERT INTO BOOKING (bookingNo, ITEMNAME, BOOKINGDATE, BOOKINGVALUE, ITEMNO, QUANTITY, BOOKEDDATE, CUSTOMER_customerNo) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
    bind => [71, Metallica, 2011-01-05 22:07:17.788, 200.0, 420, 2, 2011-01-05 22:07:17.788, 1526]
Query: InsertObjectQuery(courseworkone.Booking@201a41)
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:324)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:801)

【问题讨论】:

    标签: java jpa ejb-3.0


    【解决方案1】:

    您的客户映射使用的是默认列名“CUSTOMER_CUSTOMERNO”,但您已使用“custNo”创建表。您需要使用正确的列名。

    @ManyToOne
    @JoinColumn(name="custNo")
    public Customer getCustomer() {
        return customer;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      • 2019-07-18
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2011-01-28
      相关资源
      最近更新 更多