Hello,
I came across, what I think is a bug but I want to verify this before moving it to JIRA.
Having a uni directional 1:n relation with a key where not-null=true, the following queries are generated, when I issue
Code:
Developer developer = new Developer();
Computer c = new Computer();
developer.getComputers().add(c);
session.save(developer);
This is one query to much and when performance is an issue, this is of course a problem. You can find the mapping and classes below. The problem is the same for set, bag, list and array.
Regards Sebastian
Code:
insert into tdeveloper (name, id) values (?, ?)
insert into tcomputer (name, developer_id, listindex, id) values (?, ?, ?, ?)
update tcomputer set developer_id=?, listindex=? where id=?
Developer:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="de.laliluna.example1">
<class name="Developer" table="tdeveloper">
<id name="id">
<generator class="sequence">
<param name="sequence">tdeveloper_id_seq</param>
</generator>
</id>
<property name="name" type="string"></property>
<list name="computers" cascade="all" >
<key column="developer_id" not-null="true"></key>
<list-index column="listindex" ></list-index>
<one-to-many class="Computer" />
</list>
</class>
</hibernate-mapping>
Developer classCode:
package de.laliluna.example1;
public class Developer {
private Integer id;
private String name;
private List computers = new ArrayList(); // mapped as list
public List getComputers() {
return computers;
}
public void setComputers(List computers) {
this.computers = computers;
}
public Developer() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "Developer: " + getId() + " Name: " + getName();
}
Computer:Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="de.laliluna.example1">
<class name="Computer" table="tcomputer">
<id name="id" >
<generator class="sequence">
<param name="sequence" >tcomputer_id_seq</param>
</generator>
</id>
<property name="name" type="string"></property>
</class>
</hibernate-mapping>
Class ComputerCode:
package de.laliluna.example1;
public class Computer {
private Integer id;
private String name;
public Computer() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "Computer: " + getId() + " Name: " + getName();
}
}