我在使用SQL Server复制数据库(数据库A和数据库B)和JPA时遇到以下问题。在代码中我有这样的东西:
Object obj = new Object();
obj.setName("Nico");
objFacade.create(obj);
该对象持久存储在ID为50000的数据库B中,并将其回复到ID为1的数据库A(由SQL Server完成)。问题是,当我使用相同的对象时,例如:
obj.getId() // returns 1, should be 50000
它从数据库A返回对象的ID,由于JPA连接到数据库B,因此无法找到它。
实体中的ID配置如下:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
要暂时解决此问题,请在保留对象后运行select以从数据库中引入正确的对象。
由于
编辑:
这是persistence.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="PU-sistemas" transaction-type="JTA">
<jta-data-source>JNDI_SOS_Sistemas</jta-data-source>
<!-- Here I put all the classes of this database -->
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.cache.shared.default" value="false"/>
<property name="eclipselink.cache.size.default" value="0"/>
<property name="eclipselink.cache.type.default" value="None"/>
</properties>
</persistence-unit>
<persistence-unit name="PU-municipalidad" transaction-type="JTA">
<jta-data-source>JNDI_SOS_Municipalidad</jta-data-source>
<!-- Here I put all the classes of this database -->
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.cache.shared.default" value="false"/>
<property name="eclipselink.cache.size.default" value="0"/>
<property name="eclipselink.cache.type.default" value="None"/>
</properties>