简体   繁体   中英

'Basic' attribute type should not be Persistence Entity

I am referencing an Entity in another Entity class and getting this error. Below is sample code. I have these classes in the persistence.xml as well.

What is causing this issue? I am using Spring data JPA and Hibernate.

import javax.persistence.*;
@Entity
@Table(name = "users", schema = "university")
public class UsersEntity {
    private long id;

    @JoinColumn(name = "address_id", nullable = false)
    private Address address;

    @Id
    @Column(name = "id")
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}
import javax.persistence.*;
@Entity
@Table(name = "address", schema = "university")
public class AddressEntity {
    private long id;
    private String street;

    @Id
    @Column(name = "id")
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Basic
    @Column(name = "street")
    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }
}

So try the following:

@Entity
@Table(name = "users", schema = "university")
public class UsersEntity {
    private Long id;

    private AddressEntity address;

    @Id
    @Column(name = "id")
    public Long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

   @OneToOne 
   @JoinColumn(name = "address_id", nullable = false)
   public AddressEntity getAddress() {
        return address;
    }

    public void setAddress(AddressEntity address) {
        this.address = address;
    }
}


@Entity
@Table(name = "address", schema = "university")
public class AddressEntity {
    private Long id;
    private String street;

    @Id
    @Column(name = "id")
    public Long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }


    @Column(name = "street")
    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }
}

Basically what I did was replace the long for Long. and added the @OneToOne I removed the @Basic since its optional. I believe with that it should just work

You missed the type of relationship between entities. Eg you can define one-directional @OneToOne as

//...
public class UsersEntity {
//..
  @OneToOne(mappedBy = "ugdArea", fetch = FetchType.LAZY)
  @JoinColumn(name = "address_id", nullable = false)
  private Address address;
//...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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