提示owner_id不能为空

来源:6-2 店铺信息编辑之Service层的实现

苦做舟

2018-01-11

junit提示如下

com.imooc.o2o.exceptions.ShopOperationException: modifyShop error:addShop error:
### Error updating database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'owner_id' cannot be null
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: INSERT INTO   tb_shop(owner_id, area_id, shop_category_id,   shop_name, shop_desc, shop_addr,   phone, shop_img, priority,   create_time, last_edit_time, enable_status,   advice)   VALUES   (?,?,?,?,   ?,?,?,?,?,   ?,?, ?,?)
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'owner_id' cannot be null
; SQL []; Column 'owner_id' cannot be null; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'owner_id' cannot be null

这是什么原因呢老师

写回答

4回答

qq_一缕阳光_31

2018-02-16

同样的问题  6-4课程的时候 仍然没有看到有获取owner_id的值呀 但是提示 为空

0
0

翔仔

2018-01-11

同学好,错误非常明显啊,同学以后遇到这种问题需要根据错误仔细判断一下。。

这里说的是Column 'owner_id' cannot be null

就说列owner_id的值不能为空

调用INSERT INTO   tb_shop(owner_id, area_id, shop_category_id,   shop_name, shop_desc, shop_addr,   phone, shop_img, priority,   create_time, last_edit_time, enable_status,   advice)   VALUES   (?,?,?,?,   ?,?,?,?,?,   ?,?, ?,?) 即创建tb_shop的时候由于咱们tb_shop这张表里owner_id这个字段必须为非空,即店铺不能没有创建者的信息(不然都不知道店家是谁),因此做了这个限制不传递这个值就会报错。至于为什么没有这个值肯定是没传进来,这个同学调试就知道了 :)

0
8
精慕门1477063
回复
苦做舟
一模一样的问题。。。
2020-01-11
共8条回复

苦做舟

提问者

2018-01-11

测试方法如下

@Test
	public void testModifyShop() throws ShopOperationException,FileNotFoundException{
		Shop shop = new Shop();
		shop.setShopId(1L);
		shop.setShopName("修改后的店铺名");
		File shopImg = new File("D:\\image\\dabai.jpg");
		InputStream is = new FileInputStream(shopImg);
		ShopExecution shopExecution = shopService.modifyShop(shop, is, "dabai.jpg");
		System.out.println("新的图片地址为:" + shopExecution.getShop().getShopImg());
	}


0
0

苦做舟

提问者

2018-01-11

这是我的modifyShop方法

@Override
	public ShopExecution modifyShop(Shop shop, InputStream shopImgInputStrem, String fileName)
			throws ShopOperationException {
		if(shop == null|| shop.getShopId() == null) {
			return new ShopExecution(ShopStateEnum.NULL_SHOP);
		}else {
			try {
				//1.判断是否需要处理图片
				if(shopImgInputStrem != null && fileName!= null && !"".equals(fileName)) {
					Shop tempShop = shopDao.qureyByShopId(shop.getShopId());
					if(tempShop.getShopImg() != null) {
						ImageUtil.deleteFileOrPath(tempShop.getShopImg());
					}
					addShop(shop, shopImgInputStrem, fileName);
				}
				//2.更新店铺信息
				shop.setLastEditTime(new Date());
				int effectedNum = shopDao.updateShop(shop);
				if(effectedNum <= 0) {
					return new ShopExecution(ShopStateEnum.INNER_ERROR);
				}else {
					shop = shopDao.qureyByShopId(shop.getShopId());
					return new ShopExecution(ShopStateEnum.SUCCESS,shop);
				}
			} catch (Exception e) {
				throw new ShopOperationException("modifyShop error:" + e.getMessage());
			}
			
		}
	}

这是我的update和insert的xml文件

<insert id="insertShop" useGeneratedKeys="true" keyColumn="shop_id"
		keyProperty="shopId">
		INSERT INTO
		tb_shop(owner_id, area_id, shop_category_id,
		shop_name, shop_desc, shop_addr,
		phone, shop_img, priority,
		create_time, last_edit_time, enable_status,
		advice)
		VALUES
		(#{owner.userId},#{area.areaId},#{shopCategory.shopCategoryId},#{shopName},
		#{shopDesc},#{shopAddr},#{phone},#{shopImg},#{priority},
		#{createTime},#{lastEditTime}, #{enableStatus},#{advice})
	</insert>
	<update id="updateShop" parameterType="com.imooc.o2o.entity.Shop">
		update tb_shop
		<set>
			<if test="shopName != null">shop_name=#{shopName},</if>
			<if test="shopDesc != null">shop_desc=#{shopDesc},</if>
			<if test="shopAddr != null">shop_addr=#{shopAddr},</if>
			<if test="phone != null">phone=#{phone},</if>
			<if test="shopImg != null">shop_img=#{shopImg},</if>
			<if test="priority != null">priority=#{priority},</if>
			<if test="lastEditTime != null">last_edit_time=#{lastEditTime},</if>
			<if test="enableStatus != null">enable_status=#{enableStatus},</if>
			<if test="advice != null">advice=#{advice},</if>
			<if test="area != null">area_id=#{area.areaId},</if>
			<if test="shopCategory != null">shop_category_id=#{shopCategory.shopCategoryId}</if>
		</set>
		where shop_id=#{shopId}
	</update>


0
1
翔仔
同学好,insertshop之前还没有shop这条信息吧,那肯定也不会在insert前调用queryByShopId这个方法呀,这个方法只是为了展示店铺详情去做修改的,所以不需要有owner_id很正常啊,因为shop修改不涉及owner_id 而com.imooc.o2o.exceptions.ShopOperationException: modifyShop error:addShop error: ### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'owner_id' cannot be null ### The error may involve defaultParameterMap ### The error occurred while setting parameters ### SQL: INSERT INTO tb_shop(owner_id, area_id, shop_category_id, shop_name, shop_desc, shop_addr, phone, shop_img, priority, create_time, last_edit_time, enable_status, advice) VALUES (?,?,?,?, ?,?,?,?,?, ?,?, ?,?) ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'owner_id' cannot be null ; SQL []; Column 'owner_id' cannot be null; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'owner_id' cannot be null 这个错误明显是发生在insert shop的 和queryByShopId这个方法没啥联系呢 :)
2018-01-11
共1条回复

Java双版本(SSM到SpringBoot)校园商铺全栈开发

SSM商铺V1.0,解决毕设痛点;SpringBoot商铺V2.0,满足工作刚需

5101 学习 · 8139 问题

查看课程