更新 我似乎如果我添加时间戳它会导致问题,但我不知道为什么。我尝试了一些建议,但遗憾的是它无法正常工作
@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;
它仍然无效。我重启了玻璃鱼,它仍然无法正常工作。我去参加了一个会议,重新开始了, IT WORKED 我很生气,因为我什么也没学到,并且花了几个小时就应该花30秒。
我在Netbeans上以调试模式编辑项目,所以我希望我的所有更改都可以在文件保存时重新部署。如果不是这种情况,也许你可以解释一下。
我还尝试设置 org.hibernate.ejb.HibernatePersistence ,但我现在放弃了......)
-------------------------------------------- ------------------------
TL; DR
我已经挣扎了几个小时并阅读了我在SOF上找到的所有帖子。
为了让这个工作变得有效,我最终摧毁了我的桌子,虽然我已经定义了一个自动增量的主要ID我得到了
对象:entities.Json [id = null]不是已知的实体类型。
错误,如果我设置了值,我得到了
在回调事件上执行自动Bean验证时违反了Bean验证约束:'prePersist'。有关详细信息,请参阅嵌入式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;
Entitiy
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();
是否有人发现我列出的代码存在问题?
答案 0 :(得分:0)
有机会&#39; id&#39;字段为NULL,因为它是一个整数。如果使用int而不是Integer,则问题将得到解决。 只有当列可以为空时,才应使用Integer。 标准做法是使用原语,除非您处理泛型
阅读java中的autoboxing and unboxing以获得更多理解。