java - JPA Hibernate 保存 ManyToOne 字段为空

标签 java hibernate jpa

我有一个 servlet 方法,用于创建 JPA 实体并将现有 JPA 实体分配给 @ManyToOne 字段

当我持久化它时,它保存了实体,但外键为 NULL。为什么?

这是我的实体:

@Entity
public class SimpleEntity implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -5930519292861829894L;

    @Id @GeneratedValue
    Long id;
    String name;

    @ManyToOne()
    @JoinColumn(name="simple_entity_group_id", insertable=false, updatable=false, nullable=true)
    SimpleEntityGroup group;

    /**
     * 
     */
    public SimpleEntity() {
    }
    /**
     * @return the id
     */
    public Long getId() {
        return this.id;
    }
    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }
    /**
     * @return the name
     */
    public String getName() {
        return this.name;
    }
    /**
     * @param name the name to set
     */

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "SimpleEntity [id=" + this.id + ", name=" + this.name + ", group=" + this.getGroup() + "]";
    }
    /**
     * @return the group
     */
    public SimpleEntityGroup getGroup() {
        return this.group;
    }
    /**
     * @param group the group to set
     */
    public void setGroup(SimpleEntityGroup group) {
        this.group = group;
    }

}

@Entity
public class SimpleEntityGroup implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = -1680386377742600266L;

    @Id @GeneratedValue
    Long id;

    String name;

    @OneToMany(mappedBy="group")
    java.util.List<SimpleEntity> simpleEntities;

    /**
     * 
     */
    public SimpleEntityGroup() {
        simpleEntities = new ArrayList<SimpleEntity>();
    }
    /**
     * @return the id
     */
    public Long getId() {
        return this.id;
    }
    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }
    /**
     * @return the name
     */
    public String getName() {
        return this.name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the simpleEntities
     */
    public java.util.List<SimpleEntity> getSimpleEntities() {
        return this.simpleEntities;
    }
    /**
     * @param simpleEntities the simpleEntities to set
     */
    public void setSimpleEntities(java.util.List<SimpleEntity> simpleEntities) {
        this.simpleEntities = simpleEntities;
    }

    public void addSimpleEntity(SimpleEntity e) {
        if(this.getSimpleEntities() != null) {
            this.getSimpleEntities().add(e);
            return;
        }
        throw new RuntimeException("Entity list is null!!!");
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "SimpleEntityGroup [id=" + this.id + ", name=" + this.name + "]";
    }
    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((this.id == null) ? 0 : this.id.hashCode());
        return result;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        SimpleEntityGroup other = (SimpleEntityGroup) obj;
        if (this.id == null) {
            if (other.id != null) {
                return false;
            }
        } else if (!this.id.equals(other.id)) {
            return false;
        }
        return true;
    }


}

这是我坚持的方法:

     SimpleEntity e = new SimpleEntity();
     e.setName("Mike");
     SimpleEntityGroup g = dao.getGroupById(1l);
     e.setGroup(g);
     dao.persist(e);

     System.out.println(e);
     System.out.println(dao.findAll());

这是 Java 代码的输出,组已在条目上设置,但未保存。为什么?!?!

SimpleEntity [id=4, name=Mike, group=SimpleEntityGroup [id=1, name=Group 1]]

[SimpleEntity [id=4, name=Mike, group=null]]

最佳答案

当然我只是想出来了,需要这样做:

@ManyToOne()
@JoinColumn(name="simple_entity_group_id")
SimpleEntityGroup group;

-- 去掉了 insert=false, update=false

关于java - JPA Hibernate 保存 ManyToOne 字段为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31351263/

相关文章:

java - JPA 将属性存储为行

java - com.android.ide.common.internal.LoggedErrorException 与 Facebook SDK

java ADF - 添加弹出窗口以在离开页面时进行确认

Hibernate JPA to DDL 命令行工具

java - Hibernate - 一对多关系 - 外键始终为 "null"

java - Hibernate 5.4.4 中的 StandardQueryCache 替代方案是什么

java - 为什么 OpenJPA 集群自动生成 id?

java - Websphere 7.X。 JMX,如何启用JConsole中的所有操作?

Java:如何从抽象父类创建新的子类实例?

java - 使用 Tomcat 和 gradle hibernate