jpa多对多,附加列 [英] jpa many-to-many with additional column

查看:190
本文介绍了jpa多对多,附加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与两个实体之间的附加列有多对多的关系。在所有者端实体我已将级联类型设置为持久。

I have a many-to-many relationship with an additional column between two entities. On the owner side entity I have set the cascade type to persist.

    @OneToMany(cascade=CascadeType.PERSIST, fetch = FetchType.LAZY, mappedBy = "definitionType")
    private List<DefinitionProperty> definitionProperties = new ArrayList<DefinitionProperty>();

这是我的新实体,代表表格:

Here is my new entity which represents table:

@EmbeddedId
protected DefinitionPropertyPK pk;

@JoinColumn(name = "dtid", referencedColumnName = "id", insertable = false, updatable = false)
@ManyToOne(optional = false)
private DefinitionType definitionType;
@JoinColumn(name = "prid", referencedColumnName = "id", insertable = false, updatable = false)
@ManyToOne(optional = false)
private Property property;
@Column(name = "initial_value", insertable = false, updatable = false)
@Basic(optional = false)
private String initialValue;

问题是,当我持有所有者对象时,它将被插入到数据库中,但不会创建任何内容连接表,我得到'不能将值NULL插入列'initial_value''异常。我不知道为什么它是null因为我已经在服务代码中设置它。

The problem is when I persist the owner object it will be inserted in database but nothing will be created in join table and I get 'Cannot insert the value NULL into column 'initial_value'' Exception. I don't know why it is null because I have set it in service code.

Call: INSERT INTO definition_property (initial_value, dtid, prid) VALUES (?, ?, ?)
    bind => [3 parameters bound]

这是我的服务代码以及如何创建新对象并设置其值。

Here is my service code and how I create new objects and set their values.

DefinitionType definitionType = new DefinitionType();
        String propertyIds[] = idProperty.split(",");
        String propertyVals[] = propertyValues.split(",");
        for (int i = 0; i < propertyIds.length; i++) {
            Property property = propertyDao.find(Integer.parseInt(propertyIds[i]));
            DefinitionProperty dp = new DefinitionProperty();

            if (propertyVals[i] != "" || propertyVals[i]!=null) {
            dp.setInitialValue(propertyVals[i]);
            } else {
            dp.setInitialValue(property.getInitialValue());
        }
            dp.setDefinitionType(definitionType);
            dp.setProperty(property);
            definitionType.getDefinitionProperties().add(dp);
         }          
        definitionTypeDao.persist(definitionType);

您可以找到所有代码此处

You can find all codes here.

推荐答案

我通过将@EmbeddedId更改为@IdClass并从我的中间实体类中删除insertable = false,updatable = false来解决一切正常。

I solved by changing @EmbeddedId to @IdClass and removing insertable = false, updatable = false from my middle entity class and now everything works.

这篇关于jpa多对多,附加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆