Mongodb使用小记1之@DBRef

今天帮同事解决了一个@Dbref引用出错的问题。

Cannot create a reference to an object with a NULL id.

找了半天,在Stack Overflow上边有篇文章,深得我意。

还原场景

Document
@TypeAlias("user")
public class User {
	@DBRef
	private Billing billing;
	@DBRef
	private Role role;
	public User() {
	}

当我们在存储数据的时候,如果role和billing为null,这时候就会出现

Cannot create a reference to an object with a NULL id.

的问题。

原因是:

不能对一个值为null的id建立引用。

所以呢,解决方法是:

public User() {
		this.billing = new Billing("","");
		this.role = new Role("","");
	}
发布了29 篇原创文章 · 获赞 2 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/u012803274/article/details/80967635
今日推荐