NutzCN Logo
问答 练习一对多实例出了问题,我是新手,希望各位大牛多多指点
发布于 2995天前 作者 qq_b61182f1 1847 次浏览 复制 上一个帖子 下一个帖子
标签:

以一个主人(master)有多个宠物(Pet)为例
Pet类的代码如下

package net.wendal.nutzbook.bean;

import org.nutz.dao.entity.annotation.Column;
import org.nutz.dao.entity.annotation.Id;
import org.nutz.dao.entity.annotation.Name;
import org.nutz.dao.entity.annotation.Table;

@Table("t_pet")
public class Pet{
	@Id
	private long petId;
	@Name
	private String petName;
	@Column
	private int petAge;
	
	
	public Pet() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Pet(String petName) {
		// TODO Auto-generated constructor stub
		this.petName=petName;
	}
	public long getPetId() {
		return petId;
	}
	public void setPetId(long petId) {
		this.petId = petId;
	}
	public String getPetName() {
		return petName;
	}
	public void setPetName(String petName) {
		this.petName = petName;
	}
	public int getPetAge() {
		return petAge;
	}
	public void setPetAge(int petAge) {
		this.petAge = petAge;
	}
	
	
}
Master类的代码如下:

package net.wendal.nutzbook.bean;

import java.util.List;

import org.nutz.dao.entity.annotation.Column;
import org.nutz.dao.entity.annotation.Id;
import org.nutz.dao.entity.annotation.Many;
import org.nutz.dao.entity.annotation.Table;

@Table("t_master")
public class Master {

@Many(target=Pet.class,field="id")
private List<Pet> pets;
@Id
private long id;
@Column
private String name;
@Column
private String address;
public List<Pet> getPets() {
    return pets;
}
public void setPets(List<Pet> pets) {
    this.pets = pets;
}
public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}

}
启动服务器的时候报错了,错误信息如下:

[ERROR]-[Thread: Thread-2]-[org.nutz.mvc.impl.NutLoading.load()]: Error happend during start serivce!
java.lang.RuntimeException: Invalid @Many(field=id) 'pets' : class net.wendal.nutzbook.bean.Master<=>class net.wendal.nutzbook.bean.Pet
	at org.nutz.lang.Lang.makeThrow(Lang.java:133)
	at org.nutz.dao.impl.entity.field.ManyLinkField.<init>(ManyLinkField.java:36)
	at org.nutz.dao.impl.entity.AnnotationEntityMaker.make(AnnotationEntityMaker.java:265)
	at org.nutz.dao.impl.EntityHolder.getEntity(EntityHolder.java:69)
	at org.nutz.dao.impl.NutDao.getEntity(NutDao.java:661)
	at org.nutz.dao.util.Daos.createTablesInPackage(Daos.java:530)
	at net.wendal.nutzbook.MainSetup.init(MainSetup.java:26)
	at org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:277)
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:121)
	at org.nutz.mvc.ActionHandler.<init>(ActionHandler.java:19)
	at org.nutz.mvc.NutFilter._init(NutFilter.java:87)
	at org.nutz.mvc.NutFilter.init(NutFilter.java:65)
	at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:273)
	at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:254)
	at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:372)
	at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:98)
	at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4562)
	at org.apache.catalina.core.StandardContext$2.call(StandardContext.java:5240)
	at org.apache.catalina.core.StandardContext$2.call(StandardContext.java:5235)
	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
	at java.util.concurrent.FutureTask.run(FutureTask.java:166)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:722)

4 回复

pet不应该有个masterId吗

所有字段都要写出来,不支持隐式关联映射

如果是学习dao操作,建议看源码中的DaoUpTest类,在github上,有巨细无遗的说明

添加回复
请先登陆
回到顶部