Hello.
I have a problem with a java.util.Map, when i change the value of a key or when i put a new key/value pair the changes do not save in the data base.
Here is the database script with the tables i am mapping
Code:
CREATE TABLE app_user
(
id serial NOT NULL,
usr_name varchar(50) NOT NULL,
name varchar(100) NOT NULL,
lastname varchar(100) NOT NULL,
usr_password varchar(250) NOT NULL,
email_address varchar(250),
CONSTRAINT app_usr_pk PRIMARY KEY (id),
CONSTRAINT app_username_uq UNIQUE (usr_name)
)
CREATE TABLE field_engineer
(
id int4 NOT NULL,
ubication varchar(50) NOT NULL,
profession varchar(50) NOT NULL,
CONSTRAINT field_engineer_pk PRIMARY KEY (id),
CONSTRAINT field_engineer_inheritan_user_fk FOREIGN KEY (id)
REFERENCES app_user (id) MATCH SIMPLE
ON UPDATE RESTRICT ON DELETE RESTRICT
)
CREATE TABLE skill
(
id serial NOT NULL,
name varchar(50),
CONSTRAINT pk_skill PRIMARY KEY (id),
CONSTRAINT uq_skill_name UNIQUE (name)
)
CREATE TABLE skill_fieldengineer
(
fi_id int4 NOT NULL,
skill_id int4 NOT NULL,
score int4 NOT NULL,
CONSTRAINT pk_skill_fieldengineer PRIMARY KEY (fi_id, skill_id),
CONSTRAINT fieldengineer FOREIGN KEY (fi_id)
REFERENCES field_engineer (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT skill_id FOREIGN KEY (skill_id)
REFERENCES skill (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
I have 3 Java classes: User, FieldEngineer and Skill, the java code and are below:
User.java
Code:
package domain ;
public class User
{
private Long id;
private String userName;
private String name;
private String lastName;
private String emailAddress;
private String password;
User()
{
}
public User(String userName, String name, String lastName, String password)
{
this.userName = userName;
this.name = name;
this.lastName = lastName;
this.password = password;
}
public Long getId()
{
return id;
}
private void setId(Long id)
{
this.id = id;
}
public String getUserName()
{
return userName;
}
private void setUserName(String userName)
{
this.userName = userName;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getEmailAddress()
{
return emailAddress;
}
public void setEmailAddress(String emailAddress)
{
this.emailAddress = emailAddress;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
}
FieldEngineer.java
Code:
package domain;
import java.util.Hashtable;
import java.util.Map;
public class FieldEngineer extends User
{
private String ubication;
private String profession;
private Map skills = new Hashtable();
FieldEngineer()
{
}
public FieldEngineer(String userName, String name, String lastName, String password,
String ubication, String profession)
{
super(userName, name, lastName, password);
this.profession = profession;
this.ubication = ubication;
}
public String getUbication()
{
return ubication;
}
public void setUbication(String ubication)
{
this.ubication = ubication;
}
public String getProfession()
{
return profession;
}
public void setProfession(String profession)
{
this.profession = profession;
}
public Map getSkills()
{
return this.skills;
}
private void setSkills(Map skills)
{
this.skills = skills;
}
public void setSkill(Skill skill, int score)
{
this.getSkills().put(skill, new Integer(score));
}
}
Skill.java
Code:
package domain;
import java.util.Hashtable;
import java.util.Map;
public class Skill
{
private Long id;
private String name;
private Map fieldEngineers = new Hashtable();
Skill()
{
}
public Skill(String skillName)
{
this.name = skillName;
}
private void setName(String skillName)
{
this.name = skillName;
}
public String getName()
{
return this.name;
}
private void setFieldEngineers(Map fieldEngineers)
{
this.fieldEngineers = fieldEngineers;
}
public Map getFieldEngineers()
{
return this.fieldEngineers;
}
public Long getId()
{
return this.id;
}
private void setId(Long id)
{
this.id = id;
}
}
The xml mapping files:
User.hbm.xml
Code:
<class name="domain.User" table="app_user">
<id name="id" column="id">
<generator class="sequence">
<param name="sequence">app_user_id_seq</param>
</generator>
</id>
<property name="userName" column="usr_name" type="string" length="50"
not-null="true" unique="true" update="false"/>
<property name="name" column="name" type="string" length="100"
not-null="true" />
<property name="lastName" column="lastname" type="string" length="100"
not-null="true" />
<property name="password" column="usr_password" type="string" length="250"
not-null="true" />
<property name="emailAddress" column="email_address" type="string"
length="250" not-null="false" />
<joined-subclass name="domain.FieldEngineer" table="field_engineer">
<key column="id" />
<property name="ubication" column="ubication" type="string"
length="50" not-null="true" />
<property name="profession" column="profession" type="string"
length="50" not-null="true" />
<map name="skills" lazy="true" table="skill_fieldengineer"
order-by="score asc" inverse="true">
<key column="fi_id" />
<index-many-to-many column="skill_id" class="domain.Skill" />
<element column="score" type="integer" not-null="true" />
</map>
</joined-subclass>
</class>
Skill.hbm.xml:
Code:
<hibernate-mapping>
<class name="domain.Skill" table="skill">
<id name="id" column="id">
<generator class="sequence">
<param name="sequence">skill_id_seq</param>
</generator>
</id>
<property name="name" column="name" type="string" length="50"
not-null="true" unique="true" update="false"/>
<map name="fieldEngineers" lazy="true" table="skill_fieldengineer"
order-by="score asc" inverse="true">
<key column="skill_id" />
<index-many-to-many column="fi_id" class="domain.FieldEngineer" />
<element column="score" type="integer" not-null="true" />
</map>
</class>
</hibernate-mapping>
I can create User, FieldEngineer and Skills and save them in the data base, no problem with the session, trasaction and commit proccess. But when i modify the fieldengineer skills and i try to save it, no exception is throws but the changes are not in the database. Here is the code i am using:
Code:
Session session = TransactionManager.getSession();
TransactionManager.beginTransaction();
Criteria criteria = session.createCriteria(FieldEngineer.class);
criteria.add(Expression.eq("userName", "vanessa.rodriguez"));
FieldEngineer fieldEngineer = (FieldEngineer) criteria.uniqueResult();
criteria = session.createCriteria(Skill.class);
criteria.add(Expression.eq("name", "Comunication"));
Skill comunication = (Skill) criteria.uniqueResult();
fieldEngineer.setSkill(comunication, 100);
session.save(fieldEngineer);
TransactionManager.commitTransaction();
TransactionManager.closeSession();
As i said no exception is throwed, but when i look the skill_fieldengineer table, the value for the comunication skill of the fieldengineer is does not change, it's the previus.
The load process works fine, when i list the fieldengineer skills i can see the same values that are in the database, but the changes are not saved in the data base.
Is there something that i am missing???
Regards