Hello All,
Our project was using postgres which is now migrated to oracle.
All entities are now have sequence to fetch the new id. But i am facing
strange issue while saving object graph. Parent entity is not firing insert
statement, but child entity is firing many insert statements which number is
equivalent to number of parent. Means if if there are 2 parent entities then
(2+1) insert statement for child are firing. i verified cascade operation
is set to ALL.
Example, i have 2 entities PropertyCategory (parent table) and Property
(child table).
@Entity
public class Property implements Serializable {
private Integer propertyId;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator =
"PROP_SEQ")
@SequenceGenerator(name="PROP_SEQ", sequenceName =
"PROP_SEQ",initialValue=1,allocationSize=1)
@Column(name="property_id")
public Integer getPropertyId() {
return this.propertyId;
}
public void setPropertyId(Integer propertyId) {
this.propertyId = propertyId;
}
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="property_category_id",insertable=true,updatable=false)
public PropertyCategory getPropertyCategory() {
return this.propertyCategory;
}
// other stuff
}
@Entity
@Table(name="property_category")
public class PropertyCategory implements Serializable {
@Id
@Column(name="property_category_id")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator =
"PROP_CAT_SEQ")
@SequenceGenerator(name="PROP_CAT_SEQ", sequenceName =
"PROP_CAT_SEQ",initialValue=1,allocationSize=1)
public Integer getPropertyCategoryId() {
return this.propertyCategoryId;
}
@OneToMany(mappedBy="propertyCategory",cascade=CascadeType.ALL)
public List<Property> getProperties() {
return this.properties;
}
}
in 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="punit" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>com.nsn.bss.sao.platform.persistence.model.PropertyCategory</class>
<class>com.nsn.bss.sao.platform.persistence.model.Property</class>
<properties>
<property name="openjpa.Log"
value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE" />
<property name="openjpa.jdbc.DBDictionary"
value="JoinSyntax=sql92" />
<property name="jboss.as.jpa.providerModule"
value="org.apache.openjpa" />
<property name="openjpa.jdbc.Schemas"
value="SELFCARE4"/>
<property name="openjpa.DynamicEnhancementAgent"
value="false"/>
<property name="openjpa.RuntimeUnenhancedClasses"
value="supported" />
</properties>
</persistence-unit>
</persistence>
In my testcase ,
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/applicationContext.xml" })
@TransactionConfiguration(defaultRollback=false)
@Transactional(readOnly=false,propagation=Propagation.REQUIRES_NEW)
public class OracleTest {
@Autowired
private JpaTemplate jpaTemplate;
@Test
public void testPropertySave(){
PropertyCategory category = new PropertyCategory();
category.setPropertyCategoryName( "JUNIT" );
Property property = new Property();
property.setPropertyCategory(category);
jpaTemplate.persist( property );
}
}
in the console i can see,
12296 sao-platform TRACE [main] openjpa.jdbc.SQL - <t 26384885, conn
13563633> executing prepstmnt 11850652 SELECT t0.property_category_name FROM
SELFCARE4.property_category t0 WHERE t0.property_category_id = ? [params=?]
12429 sao-platform TRACE [main] openjpa.jdbc.SQL - <t 26384885, conn
13563633> [133 ms] spent
12461 sao-platform TRACE [main] openjpa.jdbc.SQL - <t 26384885, conn
13563633> executing prepstmnt 11357504 ALTER SEQUENCE SELFCARE4.PROP_SEQ
12486 sao-platform TRACE [main] openjpa.jdbc.SQL - <t 26384885, conn
13563633> [25 ms] spent
12486 sao-platform TRACE [main] openjpa.jdbc.SQL - <t 26384885, conn
13563633> executing prepstmnt 5493711 SELECT SELFCARE4.PROP_SEQ.NEXTVAL FROM
DUAL
12524 sao-platform TRACE [main] openjpa.jdbc.SQL - <t 26384885, conn
13563633> [38 ms] spent
12527 sao-platform TRACE [main] openjpa.jdbc.SQL - <t 26384885, conn
13563633> executing prepstmnt 24567316 ALTER SEQUENCE SELFCARE4.PROP_CAT_SEQ
12535 sao-platform TRACE [main] openjpa.jdbc.SQL - <t 26384885, conn
13563633> [7 ms] spent
12535 sao-platform TRACE [main] openjpa.jdbc.SQL - <t 26384885, conn
13563633> executing prepstmnt 15606261 SELECT SELFCARE4.PROP_CAT_SEQ.NEXTVAL
FROM DUAL
12539 sao-platform TRACE [main] openjpa.jdbc.SQL - <t 26384885, conn
13563633> [4 ms] spent
12609 sao-platform TRACE [main] openjpa.jdbc.SQL - <t 26384885, conn
13563633> executing prepstmnt 33517025 INSERT INTO SELFCARE4.Property
(property_id, derivation_expression, derived, display, multilingual, name,
readonly, data_type_id, property_category_id) VALUES (?, ?, ?, ?, ?, ?, ?,
?, ?) [params=?, ?, ?, ?, ?, ?, ?, ?, ?]
13089 sao-platform TRACE [main] openjpa.jdbc.SQL - <t 26384885, conn
13563633> [480 ms] spent
13092 sao-platform TRACE [main] openjpa.jdbc.SQL - <t 26384885, conn
13563633> executing prepstmnt 20669019 INSERT INTO SELFCARE4.Property
(property_id, derivation_expression, derived, display, multilingual, name,
readonly, data_type_id, property_category_id) VALUES (?, ?, ?, ?, ?, ?, ?,
?, ?) [params=?, ?, ?, ?, ?, ?, ?, ?, ?]
13095 sao-platform TRACE [main] openjpa.jdbc.SQL - <t 26384885, conn
13563633> [3 ms] spent
then constraint violation exception.......which is obvious since parent
hasnt inserted yet.
As you can see there are 2 insert statements for Property.
1. Am i missing any configurations in persistence.xml for this weird
behaviour ?
2. why there are alter statements for Sequence??
Thanks
Darapaneedi
--
View this message in context:
http://openjpa.208410.n2.nabble.com/OpenJPA-2-2-0-Oracle-Issue-tp7582658.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.