gpt4 book ai didi

java - 引用类 Morphia 上的索引违规

转载 作者:太空宇宙 更新时间:2023-11-04 08:10:15 24 4
gpt4 key购买 nike

我正在尝试与吗啡(0.99)/mongoDB(2)保持一对多(汽车)关系。当我尝试保留类 Car 并引用类 Owner 时,Morphia 抛出 MongoException$DuplicateKey 异常。这很奇怪,因为我认为吗啡中不存在级联持续存在。

当我持久化 Car 时,为什么吗啡会在引用的类 Owner 上的索引 *index_username* 上引发重复键异常?

波乔:

@Entity(noClassnameStored=true, value="base")
public class Base {

@Id
private ObjectId id;
...


@Entity(value = "owner", noClassnameStored = true)
@Polymorphic
public class Owner extends Base {

@Indexed(value = IndexDirection.ASC, unique=true, dropDups=true, name="index_username")
private String userName;

@Reference
private Set<Car> cars = new HashSet<Car>();
...

@Entity(value="car", noClassnameStored=true)
@Polymorphic
public class Car extends Base{
@Reference
private Owner owner

bean :

car.setOwner(owner);
BeanUtil.getDataStore().save(car);

异常(exception):

com.mongodb.MongoException$DuplicateKey: E11000 duplicate key error index: myapp.car.$index_username  dup key: { : null }

最佳答案

您不能添加多个 null 引用。为所有实体添加一个值(最多 1 个可能为 null),或者在索引上设置 sparse=true,这样您就可以拥有唯一的值,但允许多个 null

关于java - 引用类 Morphia 上的索引违规,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11360039/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com