对象不是已知的实体类型

时间:2018-06-26 13:39:31

标签: java mysql jpa

我正在使用JPA,当我创建带有继承的类时,它会给我下一个错误:

java.lang.IllegalArgumentException: Object: bookstore.domain.Item[ id=null ] is not a known entity type.
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4228)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496)

物品(父母)类别: 当我执行项目时,该表在数据库中创建。

@Entity
@Inheritance(strategy=
    InheritanceType.TABLE_PER_CLASS)
public abstract class Item implements Comparable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String title;
    private String publisher;
    private int yearPublished;
    private String ISBN;
    private double price;

    public Item (String title, String publisher, int yearPublished, String ISBN, double price){
        this.title = title;
        this.publisher = publisher;
        this.yearPublished = yearPublished;
        this.ISBN = ISBN;
        this.price = price;
    }

    public Item (){

    }

    public Long getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public int getYearPublished() {
        return yearPublished;
    }

    public void setYearPublished(int yearPublished) {
        this.yearPublished = yearPublished;
    }

    public String getISBN() {
        return ISBN;
    }

    public void setISBN(String ISBN) {
        this.ISBN = ISBN;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }



    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Item)) {
            return false;
        }
        Item other = (Item) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "bookstore.domain.Item[ id=" + id + " ]";
    }

}

图书(儿童)课程: 数据库中没有为此类创建的表

@Entity
public class Book extends Item {

private String author;
private String edition;
private String volume;

public Book(String title, String publisher, int yearPublished, String ISBN, double price, String author, String edition, String volume){
    super(title, publisher, yearPublished, ISBN, price);
    this.author = author;
    this.edition = edition;
    this.volume = volume;
}

public Book()
{

}

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}

public String getEdition() {
    return edition;
}

public void setEdition(String edition) {
    this.edition = edition;
}

public String getVolume() {
    return volume;
}

public void setVolume(String volume) {
    this.volume = volume;
}

@Override
public int compareTo(Object t) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}

请让我知道您是否知道我为什么收到此错误。

感谢您的时间!

0 个答案:

没有答案