简体   繁体   English

实体类是非实体吗?

[英]Entity Class is a non Entity?

I got the following Error: 我收到以下错误:

[class model.VerkaufterArtikel] uses a non-entity [class model.Verkauf] as target entity in the relationship attribute [field verkauf]. [class model.VerkaufterArtikel]在关系属性[field verkauf]中使用非实体[class model.Verkauf]作为目标实体。

But the class is listed in the persistence.xml. 但是该类在persistence.xml中列出。 And I tried also to not exlude unlisted classes. 而且我还尝试不排除未列出的课程。

I'm using EclipseLink as JPA Implementation. 我正在使用EclipseLink作为JPA实现。

I tried to exlude the relation but then I was not able to persist Verkauf. 我试图排除这种关系,但是后来我无法维持Verkauf。 The other classes are working correctly. 其他类正常工作。

The situation is: I have a sale(Verkauf) wich contains sold items(VerkaufteArtikel). 情况是:我有一个出售商品(Verkauf),其中包含已售商品(VerkaufteArtikel)。

model.Verkauf: model.Verkauf:

package model;


@Entity
@Table(name = "verkauf")
@NamedQuery(name = "Verkauf.findAll", query = "SELECT v FROM Verkauf v")
public class Verkauf implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private int verkID;



    private int gegeben;

    @OneToMany(mappedBy="verkauf")
    private List<VerkaufterArtikel> verkaufArtikels = new ArrayList<VerkaufterArtikel>();

model.VerkaufterArtikel: model.VerkaufterArtikel:

@Entity
@Table(name="verkauf_artikel")
@NamedQuery(name="VerkaufArtikel.findAll", query="SELECT v FROM VerkaufterArtikel v")
public class VerkaufterArtikel implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private int id;

    private float anz;

    private int artID;

    private int preispro;


    @ManyToOne
    @JoinColumn(name="verkaufID")
    private Verkauf verkauf;

My persistence XML: 我的持久性XML:

<?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="dbm" transaction-type="RESOURCE_LOCAL">
        <!-- <exclude-unlisted-classes>false</exclude-unlisted-classes>-->
        <class>model.Artikel</class>
        <class>model.ArtikelInWarenkorb</class>
        <class>model.VerkaufterArtikel</class>
        <class>model.Verkaeufer</class>
        <class>model.Verkauf</class>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mdb" />
            <property name="javax.persistence.jdbc.user" value="user" />
            <property name="javax.persistence.jdbc.password" value="password" />
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="eclipselink.allow-zero-id" value="true" />
        </properties>


    </persistence-unit>
</persistence>

And the Tables: 和表:

CREATE TABLE `verkauf_artikel` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`verkaufID` INT(11) NULL DEFAULT NULL,
`artID` INT(11) NULL DEFAULT NULL,
`anz` FLOAT NULL DEFAULT NULL,
`preispro` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`),

CREATE TABLE `verkauf` (


id` INT(11) NOT NULL AUTO_INCREMENT,
    `verkID` INT(11) NULL DEFAULT '0',
    `zeit` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `gegeben` INT(11) NOT NULL DEFAULT '0',
        PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;

Thank you! 谢谢!

I think your @ManyToOneMapping is not correct, it should look like this: 我认为您的@ManyToOneMapping不正确,应该看起来像这样:

@ManyToOne
@JoinColumn(name="verkID")
private Verkauf verkauf;

I finally found the reason for this error. 我终于找到了此错误的原因。

I had a getSum() method at the bottom of the class. 我在类的底部有一个getSum()方法。 I removed the method and everything is working fine now! 我删除了该方法,现在一切正常!

public int getSum() {

    return (int) getVerkaufArtikels()
            .stream()
            .mapToDouble(acc -> ((double) acc.getPreispro() * acc.getAnz()))
            .sum();
}

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

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