spring+hibernate+struts整合错误罗列

1.中文乱码:

JSP界面传输中文获值乱码,解决方案:工具类

public class Util{
//提供一个方法,将乱码转化成utf-8

public String getNewString(String input){
String result = “”;
try{
result = new String(input.getBytes(“iso-8859-1”),“utf-8”);
}catch(Exception e){
e.printStackTrace();
}
return result;
}

但是利用工具类检索可以,添加却是数据库一堆乱码。解决方案
在serlvers(服务器文件夹)中的server.xml中的63行添加URIEncoding=”UTF-8“这样工具类也不用了,数据库也能成中文了,一劳永逸

在这里插入图片描述

2.一对多,我用的学生多,老师一,测试检索的时候报错指向的老师映射不存在,
Hibernate:An association from the table Table refers to an unmapped class错误信息大概是这样,

解决方案:把引用的class写成全名,然后测试的时候不要用原来的开启事务new的方法,这样会丢失数据,还有就是映射文件懒加载暂时先关掉,不然获取不到值。
遍历方法,

public List<User> getAllUser() {
		
		return this.getHibernateTemplate().execute( new HibernateCallback<List<User>>() {

			@Override
			public List<User> doInHibernate(Session session) throws HibernateException, SQLException {
				String hql="from User";
				Query que=session.createQuery(hql);
				
				return que.list();
				
			}
		});

测试方法

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class UserServiceImplTest {
	@Resource(name ="us")
	private UserServices us;
	@Test
	public void test() {
	List<User> allUser = us.getAllUser();
	for (User user : allUser) {
		System.out.println(user.getName()+","+ user.getTeacher().getTname());
	}
	}

}
```测试结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201217181430991.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMwODkzNjM1,size_16,color_FFFFFF,t_70)
**注意:如果你的外键字段为空可能会报空指针**


 3.一对多创建另外一张表失败,不能自动见表,错误原因是application,xml不识别标签决绝方案是把标签全部写成全称,加上hibernate

![在这里插入图片描述](https://img-blog.csdnimg.cn/20201217175109822.png)

4.给教师表加数据,学生表外键没有对应值,大概就像这样,外键无值,
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201217175235277.png)
解决方案:
加入级联,这样就可以又数据了
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20201217175311815.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMwODkzNjM1,size_16,color_FFFFFF,t_70)


 5.给学生表加数据,反过来想让教师表里也有,但是报错
 org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: cn.oracle.pojo.Teacher; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: cn.oracle.pojo.Teacher

错误原因:因为对应表中没有这个数据,所以你是无法把游离态的数据加进去的,
解决方法:

教师类的映射取消维护
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201217181054482.png)

学生类的增加级联
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201217181117515.png)
这样就可以反向插入数据了
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值