java - JPA一直报错 "Object: entities.Json[ id=null ] is not a known Entity type."

标签 java mysql jpa-2.0

更新 我似乎如果添加时间戳就会导致问题,但我不知道为什么。我尝试了一些建议,但不幸的是它不起作用

@Basic(optional = true)
@NotNull
@Column(name="created", columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
@Temporal(TemporalType.TIMESTAMP)
private Date created;

更新#2

这是最坏的结果:问题解决了,但我不知道为什么!我将实体类更改为:

@Basic(optional = false)
@Column(name="created", insertable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date created;

但还是没用。我重启了glassfish,还是不行。我参加了一个 session ,再次重新启动,它成功了我非常恼火,因为我什么也没学到,而且在本应花费 30 秒的事情上浪费了几个小时。

我正在 Netbeans 上以 Debug模式编辑项目,因此我希望在文件保存时重新部署所有更改。如果情况并非如此,也许您可​​以提供一些线索。

我也尝试设置org.hibernate.ejb.HibernatePersistence,但我暂时放弃了......:)

-------------------------------------------------------- ------------------------

TL;DR

我已经为此奋斗了几个小时,并阅读了我能在 SOF 上找到的所有帖子。

为了让它发挥作用,我最终毁掉了我的表,尽管我已经定义了一个自动增量主 ID,但我得到了一个

对象:entities.Json[ id=null ] 不是已知的实体类型。

错误,如果我设置了值,我会得到一个

在回调事件“prePersist”上执行自动 Bean 验证时,违反了 Bean 验证约束。详细信息请参阅嵌入的 ConstraintViolations。

错误。

MySQL 表

CREATE TABLE `evaluationdb`.`json` ( `id` INT NOT NULL AUTO_INCREMENT , 
`ref_id` INT NOT NULL , 
`json` VARCHAR(4096) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , 
`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY (`id`)) 
ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;

实体

package entities;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table(name = "json")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Json.findAll", query = "SELECT j FROM Json j")
    , @NamedQuery(name = "Json.findById", query = "SELECT j FROM Json j WHERE j.id = :id")
    , @NamedQuery(name = "Json.findByRefid", query = "SELECT j FROM Json j WHERE j.refid = :refid")
    , @NamedQuery(name = "Json.findByJson", query = "SELECT j FROM Json j WHERE j.json = :json")})
public class Json implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Column(name = "refid")
    private int refid;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 4096)
    @Column(name = "json")
    private String json;

    public Json() {
    }

    public Json(Integer id) {
        this.id = id;
    }

    public Json(Integer id, int refid, String json) {
        this.id = id;
        this.refid = refid;
        this.json = json;
    }

    public Integer getId() {
        return id;
    }

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

    public int getRefid() {
        return refid;
    }

    public void setRefid(int refid) {
        this.refid = refid;
    }

    public String getJson() {
        return json;
    }

    public void setJson(String json) {
        this.json = json;
    }

    @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 Json)) {
            return false;
        }
        Json other = (Json) 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 "entities.Json[ id=" + id + " ]";
    }

}

persistance.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="com.mycompany_responseableES_war_1.0-SNAPSHOTPU" transaction-type="RESOURCE_LOCAL">
        <!--<jta-data-source>java:app/responseablees_working</jta-data-source>-->
        <class>entities.Openanswer</class>
        <class>com.company.responseablees.entities.ResponseHasOpenanswer</class>
        <class>entities.Json</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/evaluationdb"/>
            <property name="javax.persistence.jdbc.user" value="responseablees"/>
            <property name="javax.persistence.jdbc.password" value="responseablees"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <!--         <property name="eclipselink.logging.level" value="FINE"/>
            <property name="eclipselink.ddl-generation" value="create-tables"/>-->
        </properties>
    </persistence-unit>
</persistence>

代码

EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("com.mycompany_responseableES_war_1.0-SNAPSHOTPU");
EntityManager em = emfactory.createEntityManager( );
em.getTransaction().begin();
Json json = new Json();

json.setId(1);
json.setRefid(1);
json.setJson("test");

em.persist(json);
em.getTransaction().commit();

有人发现我列出的代码有问题吗?

最佳答案

“id”字段有可能为 NULL,因为它是一个整数。如果您使用 int 而不是 Integer 您的问题将得到解决。 仅当列可为空时才应使用 Integer。 标准做法是使用原语,除非您正在处理泛型

了解autoboxing and unboxing在java中以获得更多理解。

关于java - JPA一直报错 "Object: entities.Json[ id=null ] is not a known Entity type.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49736734/

相关文章:

java - 推土机映射 : More than one source to destination

mysql - 如果第三个参数为 NULL,则不要使用 SUBSTRING_INDEX

java - JPA查询不在子查询关系表中

java - 如何使用 JPA Criteria 查询来编写此查询?

orm - Spring JPA : Query builder versus Criteria Builder , 使用哪一个?

java - 未找到 Spring Bean

java - Hibernate\JPA 在高并发 Web 应用程序上的使用

java - 获取前台单元格jtable

java - 使用 php 和 JSON 将 android 连接到 mySQL

mySQL如何防止插入除非两列(字段)的组合是唯一的?