一般来说,我是 Hibernate 和 DB 的新手,所以请原谅这个基本问题。
我正在使用 DIS protocol特别是 DIS 的 Open-DIS 实现。在 DIS 中,每个 EntityStatePdu(包含模拟中实体的状态)都有一个 EntityId 对象,一个由 3 个整数组成的元组。我想将此对象用作自然 ID,并同时维护一个标准代理 ID。我的问题是我不知道如何确保数据库确定给定的 EntityId 已经存在并将该 EntityId 的主键用作 EntityStatePdu 中的外键。
换句话说,假设我有两个 EntityStatePdus,EntityID (1, 2, 3);即我们有来自同一实体的两个更新。我想要如下内容:
表格:
entity_id
pk site app entity
0 1 2 3
entity_state_pdu
pk entity_id_fk timestamp
0 0 1
1 0 2
以下是我正在测试的简化类:
@Entity
public class TestEntity {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@NaturalId
@ManyToOne(cascade = CascadeType.ALL)
private TestId naturalId;
public Long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public TestId getNaturalId() {
return naturalId;
}
public void setNaturalId(TestId naturalId) {
this.naturalId = naturalId;
}
}
和
@Entity
public class TestId {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@NaturalId
private int site;
@NaturalId
private int app;
@NaturalId
private int entity;
public TestId(int site, int app, int entity) {
this.site = site;
this.app = app;
this.entity = entity;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public int getSite() {
return site;
}
public void setSite(int site) {
this.site = site;
}
public int getApp() {
return app;
}
public void setApp(int app) {
this.app = app;
}
public int getEntity() {
return entity;
}
public void setEntity(int entity) {
this.entity = entity;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + app;
result = prime * result + entity;
result = prime * result + site;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TestId other = (TestId) obj;
if (app != other.app)
return false;
if (entity != other.entity)
return false;
if (site != other.site)
return false;
return true;
}
}
我尝试通过以下方式将两个 TestEntity 对象与两个单独的 TestId 对象(它们在具有相同站点、应用程序和实体方面是相同的)存储到数据库中:
public static void main(String[] args) {
SessionFactory factory = createFactory();
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
TestId id1 = new TestId(1,2,3);
TestEntity entity1 = new TestEntity();
entity1.setNaturalId(id1);
session.save(entity1);
tx.commit();
session.close();
Session session2 = factory.openSession();
Transaction tx2 = session2.beginTransaction();
TestId id2 = new TestId(1,2,3);
TestEntity entity2 = new TestEntity();
entity2.setNaturalId(id2);
session2.save(entity2);
tx2.commit();
session2.close();
}
我在 session2.save(entity2) 行上得到很长的堆栈跟踪,突出的行是
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '2-3-1' for key 'app'
我希望这已经足够清楚了。谢谢。
最佳答案
正如 Beatles 所说,当您告诉 Hibernate 您有一个自然键时,它会告诉 DB 强制执行唯一性。这种情况下的唯一性是通过引发您看到的类型的异常 (MySQLIntegrityConstraintViolationException) 来强制执行的。
据我所知,解决此问题的唯一方法是首先尝试获取与您的业务标识(TestId 相等)匹配的对象,然后在找到该实例时使用该实例,如果找到则使用新实例不是。 Hibernate 不会自动为您执行此操作。
关于java - Hibernate Natural ID 重复问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6058875/
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
由于fast-stemmer的问题,我很难安装我想要的任何rubygem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub
我正在尝试使用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
我意识到这可能是一个非常基本的问题,但我现在已经花了几天时间回过头来解决这个问题,但出于某种原因,Google就是没有帮助我。(我认为部分问题在于我是一个初学者,我不知道该问什么......)我也看过O'Reilly的RubyCookbook和RailsAPI,但我仍然停留在这个问题上.我找到了一些关于多态关系的信息,但它似乎不是我需要的(尽管如果我错了请告诉我)。我正在尝试调整MichaelHartl'stutorial创建一个包含用户、文章和评论的博客应用程序(不使用脚手架)。我希望评论既属于用户又属于文章。我的主要问题是:我不知道如何将当前文章的ID放入评论Controller。
我只想对我一直在思考的这个问题有其他意见,例如我有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