我们正在使用 Spring MVC 和 hibernate 开发一个 spring 应用程序。现在我们遇到了一个我们无法解决的问题。当我们试图删除一些东西时,问题就出现了。
如果我们删除该页面,则可以正常加载并继续进行,就像一切都成功一样,但数据库中的值不会被删除。
这是我们的代码:
这是 TestDao
@Repository
public class TestDaoImpl implements TestDao {
@Autowired
private SessionFactory sessionFactory;
@Override
public Test get(int id) {
return (Test)this.sessionFactory.getCurrentSession().createQuery("FROM Test WHERE id =:ident").setParameter("ident",id).uniqueResult();
}
@Override
public void delete(int id) {
this.sessionFactory.getCurrentSession().delete(this.get(id));
}
}
这是我们的服务(业务层)
@Service("testService")
public class TestServiceImpl implements TestService {
private final TestDao testDao;
@Inject
public TestServiceImpl(TestDao testDao)
{
this.testDao = testDao;
}
@Override
@Transactional
public void delete(int id) {
testDao.delete(id);
}
}
这是 Controller :
@Controller
public class TestingController {
@Qualifier("testService")
@Autowired
private TestService testService;
@RequestMapping(value = "/testing")
public ModelAndView testing()
{
testService.delete(1);
return new ModelAndView("home");
}
}
这是 hibernate 配置:
<!-- Parse database properties -->
<context:property-placeholder location="classpath:db/db.properties"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>db/mappings/User.hbm.xml</value>
<value>db/mappings/Authority.hbm.xml</value>
<value>db/mappings/Car.hbm.xml</value>
<value>db/mappings/Address.hbm.xml</value>
<value>db/mappings/DrivingDay.hbm.xml</value>
<value>db/mappings/Message.hbm.xml</value>
<value>db/mappings/Ride.hbm.xml</value>
<value>db/mappings/RouteAgreement.hbm.xml</value>
<value>db/mappings/Route.hbm.xml</value>
<value>db/mappings/RouteTime.hbm.xml</value>
<value>db/mappings/SocialMediaLogin.hbm.xml</value>
<value>db/mappings/Variables.hbm.xml</value>
<value>db/mappings/Waypoint.hbm.xml</value>
<value>db/mappings/Test.hbm.xml</value>
</list>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
我知道这是一堵代码墙,对此我很抱歉,但我想我提供了尽可能多的细节。
提前致谢
编辑:数据库中有一个id为1的值。
最佳答案
您的 xml 中可能缺少 tx:annotation-driven - 这是触发为 @Transaction 注释 bean 创建代理的那个 - 如果请求不在事务中,则删除不会工作。
关于java - Hibernate 和 spring mvc 删除和更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9296136/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html
我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
在Ruby中是否有Gem或安全删除文件的方法?我想避免系统上可能不存在的外部程序。“安全删除”指的是覆盖文件内容。 最佳答案 如果您使用的是*nix,一个很好的方法是使用exec/open3/open4调用shred:`shred-fxuz#{filename}`http://www.gnu.org/s/coreutils/manual/html_node/shred-invocation.html检查这个类似的帖子:Writingafileshredderinpythonorruby?
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht