在尝试使用hibernate和spring mvc操作ArrayList中获得的结果时,数据库中的数据会自行更改。
以下是我的代码。
public ArrayList<SellerDummy> listAllSellers() {
Session session = this.sessionFactory.getCurrentSession();
ArrayList<SellerDummy> sellerList = new ArrayList<SellerDummy>(session.createQuery("from SellerDummy").list());
for(SellerDummy sellerDummy: sellerList){
String sellerType = sellerDummy.getSellerType();
SellerType sellerType1= (SellerType) session.get(SellerType.class, Integer.parseInt(sellerType));
sellerDummy.setSellerType(sellerType1.getSellerType());
}
return sellerList;
}
这里DAOImpl类使用“listAllSellers”方法从SellerDummy表中检索数据。 SellerDummy表有一个名为“SellerType(VARCHAR)”的列,它保存相应SellerType [另一个表]的id(例如1 =“Manufacturer”,2 =“Wholesaler”)。我想要实现的是使用“SellerDummy”中提供的“SellerType”的id,进行查询以从“SellerType”获取相应的String并将其设置在从“SellerDummy”表获得的结果中。
更准确。对于例如如果我在“SellerDummy”的结果中得到“1”,它应该在ArrayList(sellerList)中将“1”更改为“Manufacturer”。代码在ArrayList和数据库中更改它。
sellerList的结果
{"id":1,"organisationName":"Ta","email":"rahul.paryani.2108@gmail.com","password":"MTIxMg==","phoneNumber":"512","address":"12","address2":"12","city":"12","state":"12","pinCode":"12","statusId":"1","sellerType":"Manufacturer"}
日志
Hibernate: select sellerdumm0_.id as id1_0_, sellerdumm0_.address as address2_0_, sellerdumm0_.address2 as address3_0_, sellerdumm0_.city as city4_0_, sellerdumm0_.email as email5_0_, sellerdumm0_.organisationName as organisa6_0_, sellerdumm0_.password as password7_0_, sellerdumm0_.phoneNumber as phoneNum8_0_, sellerdumm0_.pinCode as pinCode9_0_, sellerdumm0_.sellerType as sellerT10_0_, sellerdumm0_.state as state11_0_, sellerdumm0_.statusId as statusI12_0_ from SellerDummy sellerdumm0_
Hibernate: select sellertype0_.id as id1_1_0_, sellertype0_.sellerType as sellerTy2_1_0_ from SellerType sellertype0_ where sellertype0_.id=?
Hibernate: update SellerDummy set address=?, address2=?, city=?, email=?, organisationName=?, password=?, phoneNumber=?, pinCode=?, sellerType=?, state=?, statusId=? where id=?
我无法理解更新查询来自哪里???
谢谢和问候, 拉胡
答案 0 :(得分:0)
我认为sellerDummy
实体处于持久状态,您可以在行sellerDummy.setSellerType(sellerType1.getSellerType());
中更改它。当实体处于持久状态并且您更改它时,这些更改将在刷新会话时传播到数据库。