简体   繁体   English

合格的一对多一对多关系

[英]Qualified OneToMany-ManyToOne relation

i try to build a sample qualified relation and get a null value for the qualifier column in the join table. 我尝试建立一个示例合格关系,并为联接表中的qualifier列获取空值。

Error: 错误:

Internal Exception: java.sql.SQLIntegrityConstraintViolationException: 
cannot insert NULL into 'TIRE_POSITION'

Entities: 实体:

@Entity
public class Bike {

    @OneToMany(mappedBy = "bike", cascade=CascadeType.ALL)
    @MapKeyColumn(name="TIRE_POSITION", nullable=false)
    private Map<String, Tire> tires;

    ...
}


@Entity
public class Tire {

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="BIKE_ID")
    private Bike bike;

    public enum TirePosition{
        FRONT("FRONT"), BACK("BACK");
        ...
    }

}

Insert invocation: 插入调用:

public void testIt(){
        Bike bike = new Bike();

        Tire tireFront = new Tire();
        Tire tireBack = new Tire();
        Map<String, Tire> tires = new HashMap<String, Tire>();
        tires.put(Tire.TirePosition.BACK.getPosition(), tireBack);
        tires.put(Tire.TirePosition.FRONT.getPosition(), tireFront);
        bike.setTires(tires);

        EntityTransaction trans = em.getTransaction();
        trans.begin();
        em.persist(bike);
        trans.commit();

    }

Anyone an idea what is wrong here? 有人知道这里有什么问题吗?

Set both sides of your relationship and also remove the nullable=false constraint to see if the provider ends up setting the field. 设置关系的双方,还删除nullable = false约束,以查看提供者是否最终设置了该字段。 As JB Nizet pointed out, you are using a JoinColumn relationship instead of a relation table, which will force the "TIRE_POSITION" to be put in the Tire table; 正如JB Nizet指出的那样,您正在使用JoinColumn关系而不是关系表,这将迫使“ TIRE_POSITION”被放置在Tire表中。 Some providers (EclipseLink) will insert the Tire table with all the data from the Tire entity and then later on update the reference with mapping data contained in other entities. 某些提供程序(EclipseLink)将插入Tire表和来自Tire实体的所有数据,然后稍后使用其他实体中包含的映射数据更新引用。 Since the "TIRE_POSITION" field is mapped through Bike, it will get updated later. 由于“ TIRE_POSITION”字段是通过Bike映射的,因此稍后会更新。 If so, you can either remove the database constraint or set your database to delay constraint processing until after the transaction. 如果是这样,您可以删除数据库约束,也可以将数据库设置为延迟约束处理直到事务处理之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM