Retaining selected list item value when editing visualforce page - salesforce

Hoping someone can help with this, I have apex code which generates a select list on a Visualforce page, this works fine and the value is saved, the issue I have is that when going back to edit the page the selected value is defaulting to the first value in the list whereas it should default to the value selected.
VF page
<apex:page standardController="Service__c" extensions="ServiceParameterExtension,ServiceBroadbandController">
<style type="text/css">
.commentSize { width: 90%}
</style>
<apex:sectionheader title="{!$ObjectType.Service__c.label} Edit" subtitle="{!IF(ISNULL(Service__c.Name), 'New Service',Service__c.Name)}"
/>
<apex:pagemessages id="editPageMessage"></apex:pagemessages>
<apex:form rendered="{!contains(Service__c.Type__c, 'Change')}">
<apex:pageblock mode="edit" title="{!$ObjectType.Service__c.label} Edit">
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!Save}" />
<apex:commandButton value="Cancel" action="{!Cancel}" />
</apex:pageBlockButtons>
<apex:outputPanel >
<apex:pageBlockSection title="Information" showheader="true" columns="2" id="InformationChange">
<apex:inputField value="{!Service__c.Product__c}" />
<apex:inputField value="{!Service__c.Status__c}" />
<apex:inputField value="{!Service__c.Type__c}" />
<apex:inputField value="{!Service__c.Associated_MSA__c}" />
<apex:selectList value="{!changetypeselected}" label="Change Type" size="1">
<apex:selectOptions value="{!items}"/>
</apex:selectList>
<apex:inputField value="{!Service__c.C_I_Number__c}" label="OMS Order No." />
<apex:outputField value="{!Service__c.RecordTypeId}"/>
<apex:inputField value="{!Service__c.Ordered_Circuit_ID__c}" label="Openreach Ref." />
<apex:inputField value="{!Service__c.Product_Variant__c}" />
<apex:inputField value="{!Service__c.New_Account__c}" />
<apex:inputField value="{!Service__c.Static_IP__c}" label="Static IP Address required?"/>
<apex:inputField value="{!Service__c.Static_IP_2__c}" label="Static IP Address" />
<apex:inputField value="{!Service__c.Cancellation_Reason__c}" />
<apex:inputField value="{!Service__c.L2S__c}" />
<apex:inputField value="{!Service__c.Openreach_Cablelink__c}" />
<apex:inputField value="{!Service__c.eir_NGQ__c}" />
<apex:inputField value="{!Service__c.OMS_VIM_Circuit_ID__c}" label="OMS Circuit Ref." />
<apex:inputField value="{!Service__c.Completion_Date__c}" />
</apex:pageBlockSection>
<apex:pageBlockSection title="Order Details" showHeader="true" columns="2" id="OrderDetailsChange">
<apex:inputField value="{!Service__c.Quantity__c}" />
<apex:inputField value="{!Service__c.Account_Number__c}" />
<apex:outputLabel value=""/>
<apex:inputField value="{!Service__c.Existing_IPVPN_Circuit_ID__c}"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Comments" showheader="true" columns="1" id="CommentsChange">
<apex:inputField value="{!Service__c.Comments__c}" styleClass="commentSize"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Contracting Part Details" showHeader="true" columns="2" id="ContractingPartDetailsChange">
<apex:inputField value="{!Service__c.Site_Contact_Name__c}" required="true"/>
<apex:inputField value="{!Service__c.Welcome_Pack_Email_new__c}" />
<apex:inputField value="{!Service__c.Site_Contact_Phone_No_2__c}" />
<apex:inputField value="{!Service__c.Welcome_Pack_Email_2_new__c}" />
<apex:inputField value="{!Service__c.Site_Address__c}" style="width: 60%;"/>
<apex:inputField value="{!Service__c.Welcome_Pack_Email_3_new__c}" />
</apex:pageBlockSection>
<apex:pageBlockSection title="Admin Area" showheader="true" columns="2" id="AdminAreaChange">
<apex:outputLabel value=""/>
<apex:inputField value="{!Service__c.Approved__c}"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="System Information" showHeader="true" columns="1" id="SystemInformationChange">
<apex:outputField value="{!Service__c.RecordTypeId}"/>
</apex:pageBlockSection>
</apex:outputPanel>
</apex:pageblock>
</apex:form>
</apex:page>
Apex Controller
public with sharing class ServiceBroadbandController {
public String changetypeselected;
Service__c thisRecord;
public ServiceBroadbandController(ApexPages.standardController controller) {
thisRecord = (Service__c)controller.getRecord();
thisRecord.Static_IP__c = 'No';
}
public PageReference Save(){
if (validations()) {
thisRecord.Change_Type__c = changetypeselected;
Database.UpsertResult saveResult = Database.upsert(thisRecord, false);
if(saveResult.isSuccess()){
PageReference savePage = new PageReference('/' + thisRecord.id);
savePage.setRedirect(true);
return savePage;
}else{
for(Database.Error err : saveResult.getErrors()){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, err.getMessage()));
}
}
}
return null;
}
public List<SelectOption> getItems(){
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('','--None--'));
options.add(new SelectOption('Add_static_IP','Add Static IP'));
options.add(new SelectOption('Remove_static_IP','Remove Static IP'));
options.add(new SelectOption('Change_SLA','Change SLA'));
options.add(new SelectOption('Change_Bandwidth','Change Bandwidth'));
return options;
}
public String getchangetypeselected(){
return changetypeselected;
}
public void setchangetypeselected(String changetypeselected){
this.changetypeselected=changetypeselected;
}
}

which picklist, <apex:selectList value="{!changetypeselected}" label="Change Type" size="1">?
Try putting this in controller: changetypeselected = thisRecord.thisRecord.Change_Type__c;
It might throw something about "field was retrieved without querying", in that case you could put addFields as 1st line of constructor.
But I suspect your code is overcomplicated and you can cut a lot of apex by changing the reference in VF to mention the field explicitly: <apex:selectList value="{!Service__c.Change_Type__c}" label="Change Type" size="1">

Related

Referance a static resource via aura:attribute

I have created a new component and I want to be able to pass an image from a static resource and a custom label via aura:attribute. This is what I tried and it does not work. How can I make the image/text to show?
<aura:attribute name="profileImage" type="string" default="Standard_Profile" />
<aura:attribute name="categoryName" type="string" default="Standard_Name" />
<img src="{!$Resource + !v.profileImage}" alt="profile pic"/>
<h3>{!$Label.'categoryName'}</h3>
I am very new to Salesforce.
Map the values from your custom label and static resource as default values in the attributes and use it in the code
<aura:component implements="flexipage:AvailableForAllPageTypes">
<aura:attribute name="profileImage" type="string" default="{!$Resource.profileImage}" />
<aura:attribute name="categoryName" type="string" default="{!$Label.c.categoryName}" />
<!-- Stand-alone static resources image file-->
<img src="{!v.profileImage}"/>
<!-- custom label -->
<div>
{!v.categoryName}
</div>
</aura:component>

HTML is showing in the aura component

<aura:component implements="force:appHostable,lightning:isUrlAddressable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" controller="AccountController" access="global" >
<aura:attribute type="campaign[]" name="acctList"/>
<aura:attribute name="mycolumns" type="List"/>
<aura:handler name="init" value="{!this}" action="{!c.fetchAcc}"/>
<lightning:datatable data="{! v.acctList }"
columns="{! v.mycolumns }"
keyField="id"
hideCheckboxColumn="true"/>
</aura:component>
Could you use
<aura:unescapedHtml value=""/>

How do I get selected list values in Salesforce?

I am passing my Visualforce page the following code to have users select an employment website to post a position on:
<apex:page standardController="Position__c" extensions="PositionExtension">
<apex:form >
<apex:pageBlock title="Create New Position">
<apex:pageBlockButtons >
<apex:commandButton title="Save" value="Save" action="{!save}"/>
<apex:commandButton title="Edit" value="Edit" action="{!edit}"/>
<apex:commandButton title="Cancel" value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection columns="3" title="Basic Information">
<apex:pageBlockSectionItem >
Position Title
<apex:inputField value="{!Position__c.name}" required="true"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Record Type
<apex:inputField value="{!Position__c.recordtypeId}" required="true"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Open Date
<apex:inputField value="{!Position__c.Open_Date__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Close Date
<apex:inputField value="{!Position__c.Close_Date__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Hire By
<apex:inputField value="{!Position__c.Hire_By__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Job Type
<apex:inputField value="{!Position__c.Type__c}" required="true"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Location
<apex:inputField value="{!Position__c.Location__c}" required="true"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Travel Required?
<apex:inputField value="{!Position__c.Travel_Required__c}" required="false"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Status
<apex:inputField value="{!Position__c.Status__c}" required="true"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Functional Area
<apex:inputField value="{!Position__c.Functional_Area__c}" required="true"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Job Level
<apex:inputField value="{!Position__c.Job_Level__c}" required="true"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Job Description
<apex:inputField value="{!Position__c.Job_Description__c}" required="true"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Responsibilities
<apex:inputField value="{!Position__c.Responsibilities__c}"
required="true"/>
</apex:pageBlockSectionItem>
<br></br>
<apex:pageBlockSectionItem >
Min Pay
<apex:inputField value="{!Position__c.Min_Pay__c}"
required="true"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Max Pay
<apex:inputField value="{!Position__c.Max_Pay__c}"
required="true"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockSection title="Education and Skills" columns="3">
<apex:pageBlockSectionItem >
Educational Requirements
<apex:inputField value="{!Position__c.Educational_Requirements__c}"
required="true"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Skills Required
<apex:inputField value="{!Position__c.Skills_Required__c}" required="false"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Apex
<apex:inputField value="{!Position__c.Apex__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
C#
<apex:inputField value="{!Position__c.C_sharp__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Java
<apex:inputField value="{!Position__c.Java__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
JavaScript
<apex:inputField value="{!Position__c.JavaScript__c}"/>
</apex:pageBlockSectionItem>
<apex:selectList value="{!selected}" multiselect="true">
<apex:selectOptions value="{!websites}"/>
</apex:selectList>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:Form>
</apex:page>
And this is the code from the controller, where I'm attempting to retrieve the selected values and sort through to determine if the website was selected:
public class PositionExtension {
public Position__c pos{get; set;}
public List<Employment_Website__c> employ{get;set;}
Public List<Job_Posting__c> joblist = new List<Job_Posting__c>();
Public Set<String> selected{get;set;}
public PositionExtension (ApexPages.StandardController controller){
pos = (Position__c)Controller.getRecord();
}
//List<Job_Posting__c> postings = [SELECT Id, Employment_Website__c FROM Job_Posting__c WHERE Position__c = :pos.Id];
public List<Employment_Website__c> website{
get{return [SELECT Id, Name, web_address__c FROM Employment_Website__c];}
set{website = value;}
}
public List<SelectOption> getWebsites(){
List<SelectOption> options = new List<SelectOption>();
for (Employment_Website__c web : website){
options.add(new SelectOption(web.Id, web.name));
}
return options;
}
public Set<String> updateSelected(){
return selected;
}
public PageReference save(){
//List<Job_Posting__c> postings = [SELECT Id, Employment_Website__c FROM Job_Posting__c WHERE Position__c = :pos.Id];
upsert pos;
for(Employment_Website__c web : website){
system.debug(selected);
if(selected.contains(web.Id)){
Job_Posting__c posting = new Job_Posting__c();
posting.Employment_Website__c = web.Id;
posting.Position__c = pos.Id;
joblist.add(posting);
}
}
upsert joblist;
PageReference acctPage = new ApexPages.StandardController(pos).view();
acctPage.setRedirect(true);
return acctPage;
}
}
Any suggestions on how to get the selected values back into the controller to create a job posting? Let me know if I should post more of my Visualforce code. Thanks!
You need to convert data type for selected.
Current :
Public Set selected{get;set;}
Updated
Public List selected{get;set;}

Hibernate table doesn't exist

When I test my project, just 4 tables are created in the database but not the others and I do not know why. The tables notification, position, dernierePosition and user are created but not the table demande and other tables that I didn't put in this example. There are some properties that i forgot?
Thanks for your help.
Here are some files:
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="">
<!-- Database connection settings, Connect to HSQL, IN Memory -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/***</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.username">***</property>
<property name="hibernate.connection.password"/>
<!-- DB schema will be updated if needed -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- JDBC connection pool (use the built-in)
List of XML mapping files -->
<mapping class="modele.Demande" resource="Demande.hbm.xml"/>
<mapping class="modele.DernierePosition" resource="DernierePosition.hbm.xml"/>
<mapping class="modele.Group" resource="Group.hbm.xml"/>
<mapping class="modele.Invitation" resource="Invitation.hbm.xml"/>
<mapping class="modele.Marqueur" resource="Marqueur.hbm.xml"/>
<mapping class="modele.Notification" resource="Notification.hbm.xml"/>
<mapping class="modele.NotificationMarqueur" resource="NotificationMarqueur.hbm.xml"/>
<mapping class="modele.Position" resource="Position.hbm.xml"/>
<mapping class="modele.User" resource="User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Position.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 15 févr. 2014 01:46:28 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="modele.Position" table="POSITION">
<id name="id" type="int">
<column name="ID" />
<generator class="native" />
</id>
<property name="lattitude" type="double">
<column name="LATTITUDE" />
</property>
<property name="longitude" type="double">
<column name="LONGITUDE" />
</property>
</class>
</hibernate-mapping>
DenierePosition.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 15 févr. 2014 01:46:28 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<joined-subclass name="modele.DernierePosition" extends="modele.Position" table="DERNIEREPOSITION" lazy="false">
<key>
<column name="ID" />
</key>
<property name="time" type="java.util.Date">
<column name="TIME" />
</property>
<one-to-one name="user" class="modele.User"></one-to-one>
</joined-subclass>
</hibernate-mapping>
Notification.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 15 févr. 2014 01:46:28 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="modele.Notification" table="NOTIFICATION">
<id name="id" type="int">
<column name="ID" />
<generator class="native" />
</id>
<many-to-one name="user" class="modele.User" fetch="join">
<column name="USER" />
</many-to-one>
</class>
</hibernate-mapping>
Demande.java
#XmlRootElement
public class Demande extends Notification {
private Group group;
private User demandeur;
public Demande(){
}
public Demande(Group group, User demandeur) {
super(group.getProprietaire());
this.group = group;
this.demandeur=demandeur;
}
// Getters and setters
}
Demande.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 15 févr. 2014 01:46:28 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<joined-subclass name="modele.Demande" extends="modele.Notification" table="DEMANDE" lazy="false">
<key>
<column name="ID" />
</key>
<many-to-one name="group" class="modele.Group" fetch="join">
<column name="GROUP" />
</many-to-one>
<many-to-one name="demandeur" class="modele.User" fetch="join">
<column name="DEMANDEUR" />
</many-to-one>
</joined-subclass>
</hibernate-mapping>
User.java
#XmlRootElement
public class User {
/** Attributs */
private int id;
private String pseudo;
private String telephone;
private String email;
#XmlTransient
private Set<Demande> demandes;
#XmlTransient
private Set<Invitation> aInvite;
#XmlTransient
private Set<Notification> notifications;
private DernierePosition dernierePosition;
#XmlTransient
private Set<Group> groups;
#XmlTransient
private Set<Group> proprietaire;
/** Constructeur */
public User() {
}
public User(String telephone, String pseudo, String email) {
super();
this.pseudo = pseudo;
this.telephone = telephone;
this.email = email;
}
// Getters and setters
}
User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 15 févr. 2014 01:46:28 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="modele.User" table="USER">
<id name="id" type="int">
<column name="ID" />
<generator class="native" />
</id>
<property name="pseudo" type="java.lang.String">
<column name="PSEUDO" />
</property>
<property name="telephone" type="java.lang.String" not-null="true" unique="true">
<column name="TELEPHONE" />
</property>
<property name="email" type="java.lang.String">
<column name="EMAIL" />
</property>
<set name="demandes" table="DEMANDE" inverse="false" lazy="true">
<key>
<column name="ID" />
</key>
<one-to-many class="modele.Demande" />
</set>
<set name="aInvite" table="INVITATION" inverse="false" lazy="true" access="field">
<key>
<column name="ID" />
</key>
<one-to-many class="modele.Invitation" />
</set>
<set name="notifications" table="NOTIFICATION" inverse="false" lazy="true">
<key>
<column name="ID" />
</key>
<one-to-many class="modele.Notification" />
</set>
<one-to-one name="dernierePosition" class="modele.DernierePosition"></one-to-one>
<set name="groups" table="GROUP" inverse="false" lazy="true">
<key>
<column name="ID" />
</key>
<one-to-many class="modele.Group" />
</set>
<set name="proprietaire" table="GROUP" inverse="false" lazy="true">
<key>
<column name="ID" />
</key>
<one-to-many class="modele.Group" />
</set>
</class>
</hibernate-mapping>
Group.java
#XmlRootElement
public class Group {
/** Attributs */
private int id;
private String description;
private String hashtag;
private String password;
private Set<Marqueur> marqueurs;
#XmlTransient
private Set<Invitation> invitations;
#XmlTransient
private Set<Demande> demandes;
private User proprietaire;
private Set<User> invites;
/**
* Constructeur
* */
public Group()
{
}
public Group(String description, String hashtag, User proprietaire, String password) {
super();
this.description = description;
this.hashtag = hashtag;
this.proprietaire = proprietaire;
this.setPassword(password);
}
Group.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 16 f?vr. 2014 11:30:30 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="modele.Group" table="GROUP">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="description" type="java.lang.String">
<column name="DESCRIPTION" />
</property>
<property name="hashtag" type="java.lang.String">
<column name="HASHTAG" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" />
</property>
<set name="marqueurs" table="MARQUEUR" inverse="false" lazy="true">
<key>
<column name="ID" />
</key>
<one-to-many class="modele.Marqueur" />
</set>
<set name="invitations" table="INVITATION" inverse="false" lazy="true">
<key>
<column name="ID" />
</key>
<one-to-many class="modele.Invitation" />
</set>
<many-to-one name="proprietaire" class="modele.User" fetch="join">
<column name="PROPRIETAIRE" />
</many-to-one>
<set name="invites" table="USER" inverse="false" lazy="true">
<key>
<column name="ID" />
</key>
<one-to-many class="modele.User" />
</set>
</class>
</hibernate-mapping>
Test User
package dao;
import java.util.List;
import junit.framework.Assert;
import modele.User;
import org.junit.Test;
public class UserDaoTest {
private final String tel = "0601020304";
private final String pseudo = "pseudo";
private final String pseudoModified = "pseudo2";
private final String email = "email#email.com";
#Test
public void addUserTest(){
User initialUser = new User(tel, pseudo, email);
UserDao.addUser(initialUser);
User addedUser = UserDao.getUser(tel);
Assert.assertEquals(addedUser.getTelephone(), initialUser.getTelephone());
Assert.assertEquals(addedUser.getPseudo(), initialUser.getPseudo());
Assert.assertEquals(addedUser.getEmail(), initialUser.getEmail());
}
#Test
public void modifyUserTest(){
UserDao.modifyUser(tel, pseudoModified, email);
User modifiedUser = UserDao.getUser(tel);
Assert.assertEquals(modifiedUser.getTelephone(), tel);
Assert.assertEquals(modifiedUser.getPseudo(), pseudoModified);
Assert.assertEquals(modifiedUser.getEmail(), email);
}
#Test
public void listUserTest(){
User modifiedUser = UserDao.getUser(tel);
List<User> listUsers = UserDao.listUser();
User listedUser = listUsers.get(listUsers.indexOf(modifiedUser));
Assert.assertEquals(listedUser.getTelephone(), modifiedUser.getTelephone());
Assert.assertEquals(listedUser.getPseudo(), modifiedUser.getPseudo());
Assert.assertEquals(listedUser.getEmail(), modifiedUser.getEmail());
}
#Test
public void deleteUserTest(){
User user = UserDao.getUser(tel);
UserDao.deleteUser(user.getTelephone());
List<User> listUsers = UserDao.listUser();
Assert.assertEquals(listUsers.indexOf(user), -1);
}
}
It seems that there's a problem with many-to-one and joined-subclass. It's explained in detail over on the hibernate forums; I'm not entirely clear on why it's the case, but the explanation from sdknott (on that site) is:
The problem you have is with your mapping. By stating that your constraining column of the many-to-one is in fact your primary key column of table A, Hibernate will always try and resolve an entity B since the many-to-one column will never be empty.
I've used your source and changed the various many-to-one mappings in joined-subclasses to one-to-one mappings, and it all works for me now. Hopefully that will allow you to express your schema correctly.
(Aside) Original answer that didn't solve the problem
I see that you have two members called "user" in the Demande class; remember that Demande is a subclass of Notification and gets its members. I've never tried that and it looks odd, and it's definitely going to be confusing. It might be what's causing your problem.
If you really want two "user" members, give them more suitable names (maybe "requester" and "responder"? Just guessing from the context). If you only want one "user", then you don't need to put it in Demande, it's already in Notification.

Hibernate mapping and merging

I have 2 classes like this :
Message(id, title, content)
MessageEmployee(id, messageId, employeeId, readFlag)
and 2 tables like this :
MESSAGE(mess_id, mess_title, mess_content)
MESSAGE_EMPLOYEE(mess_empl_id, mess_id, empl_id, read_fg)
Mapping files :
<hibernate-mapping package="core">
<class name="Message" table="MESSAGE">
<!-- class id -->
<id name="id" type="int" column="MESS_ID" length="11">
<generator class="native"/>
</id>
<property name="content" type="string" column="MESS_CONTENT" />
<property name="title" type="string" column="MESS_TITLE" />
</class>
</hibernate-mapping>
<hibernate-mapping package="core">
<class name="MessageEmployee" table="MESSAGE_EMPLOYEE">
<!-- class id -->
<id name="id" type="int" column="MESS_EMPL_ID" length="11">
<generator class="native"/>
</id>
<!-- employee -->
<many-to-one name="employee" class="core.Employee"
column="EMPL_ID" cascade="save-update,merge" lazy="false" />
<!-- message -->
<many-to-one name="message" class="core.Message"
column="MESS_ID" cascade="save-update,merge" lazy="false" />
<property name="readFlag" type="character" column="READ_FG" />
</class>
</hibernate-mapping>
Here is my problem :
let's say I already have a message in database, and I want to create a messageEmployee and save it.
Code snippet :
Message sent = new Message(content, title);
Employee e = employeeDao.loadEmployeeWithId(Integer.valueOf(to));
messageDao.merge(sent)
MessageEmployee m = new MessageEmployee(sent, e, null);
m.setReadFlag('N');
messageEmployeeDao.mergeMessageEmploye(m);
When I merge(messageEmployee), it creates a new message and a new messageEmployee in database, but I don't want it to create a new message.
I'm quite sure my mapping is wrong since I am no expert, so what could I change to get the behaviour I want ?
It will create a new Message object because you are not providing id in your message object sent. When you execute messageDao.merge(sent), the sent object doesn't have and id to merge with. It is pure transient object. Try loading it from database like Employee.

Resources