小编Raf*_*afe的帖子

为什么Hibernate在没有插入子行的情况下插入带有外键的父行?

我希望有人之前遇到过这个问题,可以帮助我.基本上,Hibernate插入父行(ID指向子行),但不插入具有关联ID的子行,这使数据库处于错误状态.这是Hibernate尝试加载未正确保存的对象时抛出异常的示例:

27 Jun 2011 13:55:31,380 ERROR [scheduler_Worker-4] - 
Job DEFAULT.queryScrubJobDetail threw an unhandled Exception: 
org.springframework.scheduling.quartz.JobMethodInvocationFailedException: 
Invocation of method 'doIt' on target class [XXX] failed; nested exception is
org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException: 
No row with the given identifier exists: 
[XXX.DataProviderTransaction#60739703]; nested exception is org.hibernate.ObjectNotFoundException: 
No row with the given identifier exists: 
[com.idology.persist.DataProviderTransaction#2]
Run Code Online (Sandbox Code Playgroud)

这部分应用程序有三个实体:

  • Query,它是DataProviderTransactionReference和DataProviderTransaction的父级
  • DataProviderTransaction,它是Query的子项,也是DataProviderTransactionReference的父项
  • DataProviderTransactionReference,其外键指向DataProviderTransaction和Query

以下是映射:

来自查询:

@OneToMany(mappedBy = "query", cascade =
    { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
@JoinColumn(name = "query_id")
public List<DataProviderTransactionReference> getDataProviderTransactionReferences() …
Run Code Online (Sandbox Code Playgroud)

java mysql orm hibernate

7
推荐指数
1
解决办法
2678
查看次数

为什么我的 Rails 测试看不到内存 SQLite3 数据库的内容?

我试图通过使用 SQLite3 内存数据库作为应用程序的测试数据库来加快 Rails 应用程序的测试速度。我已按照这篇博文中的说明进行操作。

我的数据库配置database.yml如下所示:

test:
  adapter: sqlite3
  database: ":memory:"
  encoding: utf8
  verbosity: quiet
Run Code Online (Sandbox Code Playgroud)

我还按照建议创建了初始化程序:

def in_memory_database?
  Rails.env == "test" and
    ActiveRecord::Base.connection.class == ActiveRecord::ConnectionAdapters::SQLiteAdapter ||
      ActiveRecord::Base.connection.class == ActiveRecord::ConnectionAdapters::SQLite3Adapter and
    Rails.configuration.database_configuration['test']['database'] == ':memory:'
end

if in_memory_database?
  ActiveRecord::Schema.verbose = false
  puts "creating sqlite in memory database"
  load "#{Rails.root}/db/schema.rb"
end
Run Code Online (Sandbox Code Playgroud)

我的所有测试都失败了,因为无法加载装置,如您所见:

creating sqlite in memory database
Loaded suite test/unit/vendor_appliance_test
Started
E
Finished in 0.070079 seconds.

  1) Error:
test_the_truth(VendorApplianceTest):
ActiveRecord::StatementInvalid: Could not find table 'advertisers'


1 tests, 0 assertions, …
Run Code Online (Sandbox Code Playgroud)

sqlite testing ruby-on-rails

5
推荐指数
1
解决办法
1532
查看次数

你如何防止Rails在FasterCSV输出中编码实体?

我正在使用FasterCSV在Rails 3应用程序中生成报告的CSV输出.这是一段代码片段:

<%= FasterCSV.generate do |csv|
  @groups.each do |b|
    record = [ b.group, b.organization_name, b.status, b.comments ]
    csv << record
  end
end 
%>
Run Code Online (Sandbox Code Playgroud)

当FasterCSV包含空字符串时,它使用一对空的双引号.

不幸的是,Rails 3将这些引号编码为实体,这对Excel不起作用.这是我的CSV输出的样子(当b.comments为nil或空字符串时):

Rafeland,Rafe Organization,Submitted,&quot;&quot;
Run Code Online (Sandbox Code Playgroud)

什么是阻止Rails编码这些实体的普遍接受的方法?我知道"原始"方法,但它不会阻止我可以将CSV生成.

csv ruby-on-rails fastercsv

3
推荐指数
1
解决办法
563
查看次数

标签 统计

ruby-on-rails ×2

csv ×1

fastercsv ×1

hibernate ×1

java ×1

mysql ×1

orm ×1

sqlite ×1

testing ×1