繁体   English   中英

异常描述CustomerTransaction]在关系属性[field id]中使用非实体[class java.lang.Long]作为目标实体

[英]Exception Description CustomerTransaction] uses a non-entity [class java.lang.Long] as target entity in the relationship attribute [field id]

我正在尝试使用JPA创建我的第一个项目,并且在声明@manytoone关系时遇到了一些麻烦。 这似乎是一个常见的问题,我已经尝试了几种解决方案,但没有运气。

背后的逻辑是,一个客户可以有很多交易,但是一个交易只能有一个客户。

这是我的代码。 客户交易

@Entity

@Table(name = "CustomerTransaction")//Specifiying the table name
public class CustomerTransaction   implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
   /*Declaring the relation ManytoOne and joining customer basic information*/

    @ManyToOne 
    @JoinColumns({

                        @JoinColumn(name = "firstName;", referencedColumnName = "firstName;"),
                        @JoinColumn(name = "lastName;", referencedColumnName = "lastName;"),

        })

客户等级

@Entity
public class Customer implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)

    //Customer attributes
    private Long id ;
    private String firstName;
    private String lastName;
    private String customerEmail; 
    private String phone; 
    private String customerID;

    //Declaring one to many relation
    @OneToMany (cascade = {CascadeType.PERSIST, CascadeType.REMOVE}) 
    private List<CustomerTransaction> customerT; //List of customer transactions

坚持“以防万一”

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="MyPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>com.mycompany.carsales.Customer</class>
    <class>com.mycompany.carsales.Cars</class>
    <class>com.mycompany.carsales.CustomerTransaction</class>
    <class>com.mycompany.carsales.FamilyCars</class>
    <class>com.mycompany.carsales.SportCars</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/Cars;create=true"/>
      <property name="javax.persistence.jdbc.password" value="app"/>
      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
      <property name="javax.persistence.jdbc.user" value="app"/>
      <property name="eclipselink.ddl-generation" value="create-tables"/>
      <property name="javax.persistence.schema-generation.database.action" value="create"/>
    </properties>
  </persistence-unit>
</persistence>

错误信息

Exception Description: [class com.mycompany.carsales.CustomerTransaction] uses a non-entity [class java.lang.Long] as target entity in the relationship attribute [field id].

您的两个类都需要正确注释或具有正确的字段。 您的第一个问题是,CustomerTransaction不会忽略您正在注释的字段。 其次,您需要在联接列中注释要引用的客户字段,并添加反向联接列以供参考。

CustomerTransaction类:

@Entity
@Table(name = "customer_transaction")  //Specifiying the table name
    public class CustomerTransaction   implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
   /*Declaring the relation ManytoOne and joining customer basic information*/
    private Long id;

    @ManyToOne 
    @JoinTable(name = "customer_transaction_join",
          joinColumns = @JoinColumn(name = "transaction_id", referencedColumnName = "id"),
          inverseJoinColumns = @JoinColumn(name = "customer_id", referencedColumnName="id"), 
          uniqueConstraints = @UniqueConstraint(columnNames = {"transaction_id", "customer_id"}))

    })
    private Customer customer;

请注意uniqueConstraints-不允许重复输入,这意味着一笔交易只能有一个客户。

客户类别:

@Entity
@Table(name = "customer")
public class Customer implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Column(name = "customer_email")
    private String customerEmail; 

    @Column(name = "phone")
    private String phone; 

    @Column(name = "customer_id")
    private String customerID;

    //Declaring one to many relation
    @OneToMany (cascade = {CascadeType.PERSIST, CascadeType.REMOVE}) 
    private List<CustomerTransaction> customerTransations;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM