NutzCN Logo
问答 请问下有一对多多对一等对应关系自动建表报错
发布于 101天前 作者 yuffo 117 次浏览 复制 上一个帖子 下一个帖子
标签:

这样自动建表
主模块初始化时

	public void init() {
		Daos.createTablesInPackage(dao, "io.nutz.demo.po",false);
		// NB自身初始化完成后会调用这个方法
	}

主表

@Table("master")
public class Master {
	@Id
	Long id;

	@Column
	@Many
	private Pet pets;
//getting / setting ....
}

从表

@Table("pet")
public class Pet {
	@Id
	Long id;
	@One
	@Column
	private Master master;
//getting / setting ....
}

这样是不能建表的吗
会报异常

[DEBUG] 17:38:09.624 org.nutz.resource.Scans.scan(Scans.java:280) - Found 0 resource by src( sqls/ ) , regex( .(sql|sqlx|sqls)$ )
[DEBUG] 17:38:09.625 org.nutz.dao.impl.DaoSupport.setSqlManager(DaoSupport.java:121) - SqlManager Sql count=0
[DEBUG] 17:38:09.626 org.nutz.resource.Scans.scan(Scans.java:280) - Found 2 resource by src( io/nutz/demo/po/ ) , regex( ^.+[.]class$ )
[DEBUG] 17:38:09.637 org.nutz.ioc.impl.ScopeContext.remove(ScopeContext.java:84) - Remove object 'mainLauncher' from [app] 
[ERROR] 17:38:09.637 org.nutz.boot.NbApp._run(NbApp.java:202) - something happen!!
org.nutz.ioc.IocException: IocBean[mainLauncher] throw Exception when creating
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:152)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:269)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:159)
	at org.nutz.boot.NbApp._run(NbApp.java:193)
	at org.nutz.boot.NbApp.run(NbApp.java:170)
	at io.nutz.demo.MainLauncher.main(MainLauncher.java:38)
Caused by: org.nutz.dao.DaoException: Invalid @One(field=masterId) 'master' : class io.nutz.demo.po.Pet<=>class io.nutz.demo.po.Master
	at org.nutz.dao.impl.entity.field.OneLinkField.<init>(OneLinkField.java:38)
	at org.nutz.dao.impl.entity.AnnotationEntityMaker.make(AnnotationEntityMaker.java:290)
	at org.nutz.dao.impl.EntityHolder.getEntity(EntityHolder.java:72)
	at org.nutz.dao.impl.entity.field.AbstractLinkField.getLinkedEntity(AbstractLinkField.java:72)
	at org.nutz.dao.impl.entity.field.ManyLinkField.<init>(ManyLinkField.java:26)
	at org.nutz.dao.impl.entity.AnnotationEntityMaker.make(AnnotationEntityMaker.java:294)
	at org.nutz.dao.impl.EntityHolder.getEntity(EntityHolder.java:72)
	at org.nutz.dao.impl.NutDao.getEntity(NutDao.java:682)
	at org.nutz.dao.util.Daos$3.compare(Daos.java:464)
	at org.nutz.dao.util.Daos$3.compare(Daos.java:462)
	at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355)
	at java.util.TimSort.sort(TimSort.java:220)
	at java.util.Arrays.sort(Arrays.java:1512)
	at java.util.ArrayList.sort(ArrayList.java:1454)
	at java.util.Collections.sort(Collections.java:175)
	at org.nutz.dao.util.Daos.createTablesInPackage(Daos.java:462)
	at io.nutz.demo.MainLauncher.init(MainLauncher.java:30)
	at io.nutz.demo.MainLauncher$FM$init$62febe830a4a6764c634d9e263964cee.invoke(MainLauncher.java)
	at org.nutz.ioc.impl.ObjectMakerImpl$2.trigger(ObjectMakerImpl.java:180)
	at org.nutz.ioc.weaver.DefaultWeaver.onCreate(DefaultWeaver.java:89)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:141)
	... 6 more
[DEBUG] 17:38:09.640 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:164) - Get 'jettyStarter'<interface org.nutz.boot.starter.ServerFace>
[INFO ] 17:38:09.642 org.eclipse.jetty.server.AbstractConnector.doStop(AbstractConnector.java:331) - Stopped ServerConnector@210ab13f{HTTP/1.1,[http/1.1]}{127.0.0.1:8080}

然后如果我在从表Pet加上这个
@Column
Long masterId;
就能建表成功了

想问下原因,因为我已经写了
@One
@Column
private Master master;
了没有masterId不能自动帮我建一个的吗

4 回复

@One/@Many不能写在同一属性,关联字段要单独写出来,不支持隐式映射

好的,是我当成hibernate了,那如果要关联一个已存在的表,也是设置这个字段吗
比如Pet要关联一个已存在的Master
就设置pet.MasterId字段
设置pet.Master.Id是没用的对吧

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