jpa保持“object:entities.json[id=null]不是已知的实体类型”

bjg7j2ky  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(339)

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

@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,它仍然不起作用。我去参加了一个会议,重新启动了它,它工作了。我很恼火,因为我什么也没学到,在一些本该花30秒的事情上浪费了几个小时。
我在调试模式下编辑netbeans上的项目,所以我希望所有的更改都能在文件保存时重新部署。如果不是这样的话,也许你能给我们一些启示。
我还试图设置org.hibernate.ejb.hibernatepersistence,但我放弃了。。。目前:)

dz6r00yl

dz6r00yl1#


热释光;博士
我已经为此奋斗了几个小时,并且阅读了所有我能在sof上找到的帖子。
为了让它正常工作,我最终对我的表进行了大量的破坏,虽然我定义了一个自动递增的主id,但我得到了一个
object:entities.json[id=null]不是已知的实体类型。
错误,如果我设置了值
对回调事件“prepersist”执行自动bean验证时违反了bean验证约束。有关详细信息,请参阅嵌入式约束。
错误。
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 + " ]";
    }

}

持久性.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();

有人看到我列出的代码有问题吗?

hi3rlvi2

hi3rlvi22#

“id”字段可能为空,因为它是整数。如果您使用int而不是integer,您的问题将得到解决。仅当列可为空时才应使用整数。标准实践是使用原语,除非您处理的是泛型
阅读有关java中的自动装箱和取消装箱的更多信息。

相关问题