NutzCN Logo
问答 pojo extends 问题
发布于 3254天前 作者 javanan 1970 次浏览 复制 上一个帖子 下一个帖子
标签:

我有 A B 两个表,结构完成一样
Apojo

@Table("a")

public class APojo {

	@Name
	protected String id; // int(11) NOT NULL

	@Column("name")
	protected String names = "名字";// char(10) NOT NULL

	@Column("age")
	protected int age;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getNames() {
		return names;
	}

	public void setNames(String names) {
		this.names = names;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}; // int(11) NULL
	
	
}

Bpojo

@Table("b")

public class BPojo extends APojo {


	
}

业务逻辑是 假设 A中不存在某个记录, 去B表找,并且插入 A 表

@At("/feinto")
	public void feinto() {
		APojo apojo = dao.fetch(APojo.class, Cnd.where("id", "=", "479db588067e43828be9cd3f045b4d6f"));
		if (apojo == null) {
			System.out.println("null-----------------------");
			apojo = dao.fetch(BPojo.class, Cnd.where("id", "=", "479db588067e43828be9cd3f045b4d6f"));
			System.out.println(apojo.getId());
			System.out.println(apojo.getAge());
			System.out.println(apojo.getNames());
		}
		dao.insert(apojo);
	}

蛋疼的是,,,出现 id 异常,
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '479db588067e43828be9cd3f045b4d6f' for key 'PRIMARY'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041)

9 回复

从 日志上来看
把 B复制给 A
apojo = dao.fetch(BPojo.class, Cnd.where("id", "=", "479db588067e43828be9cd3f045b4d6f"));
最后插入的还是
B
dao.insert(apojo);

Caused by: org.nutz.dao.DaoException: !Nutz SQL Error: 'INSERT INTO b(id,name,age) VALUES(?,?,?)
-----

"把 B复制给 A",那是赋值!!!!

@wendal 有 什么办法吗?

BeanUtil.copyProperties

或者取nutz快照版里面的 Lang.copyProperties

@wendal 没懂
BeanUtil.copyProperties

或者取nutz快照版里面的 Lang.copyProperties
没找到对应的方法!

@wendal 另外一个 -
源码
public Entity<?> getEntity() {
return entity;
}

不是返回 对应pojo的 @Table 里面的声明吗?

B赋值给了 A  ---A指向 B  ,,? 取的是B的?

改用这样了

@At("/feinto") public void feinto() { APojo apojo = aservice.fetch(Cnd.where("id", "=", "479db588067e43828be9cd3f045b4d6f")); if (apojo == null) { apojo=new APojo(); BPojo bpojo = bservice.fetch(Cnd.where("id", "=", "479db588067e43828be9cd3f045b4d6f")); Mirror<BPojo> mirror = Mirror.me(BPojo.class); Field[] fields = mirror.getFields(); for (int i = 0; i < fields.length; i++) { mirror.setValue(apojo, fields[i].getName(), mirror.getValue(bpojo, fields[i].getName())); } } dao.insert(apojo); }



public static <T> T copyProperties(Object origin, T target) { return copyProperties(origin, target, null, null, false, true); } public static <T> T copyProperties(Object origin, T target, String active, String lock, boolean ignoreNull, boolean ignoreStatic) { if (origin == null) throw new IllegalArgumentException("origin is null"); if (target == null) throw new IllegalArgumentException("target is null"); Pattern at = active == null ? null : Pattern.compile(active); Pattern lo = lock == null ? null : Pattern.compile(lock); Mirror<Object> originMirror = Mirror.me(origin); Mirror<T> targetMirror = Mirror.me(target); Field[] fields = targetMirror.getFields(); for (Field field : originMirror.getFields()) { String name = field.getName(); if (at != null && !at.matcher(name).find()) continue; if (lock != null && lo.matcher(name).find()) continue; if (ignoreStatic && Modifier.isStatic(field.getModifiers())) continue; Object val = originMirror.getValue(origin, field); if (ignoreNull && val == null) continue; for (Field _field : fields) { if (_field.getName().equals(field.getName())) { targetMirror.setValue(target, _field, val); } } // TODO 支持getter/setter比对 } return target; }
添加回复
请先登陆
回到顶部