Maintaining DTO Identities with @FXKey and uid Property

The correlation between the instances of the Java and ActionScript DTO and, indirectly, between ActionScript DTO in multiple instances of the Flex application is driven by uid, which is a mandatory property for each DTO. The uid must have a unique String value in each DTO. Implementing the uid in the Java entities and custom DTOs is your responsibility. CDB automatically implements uid in dynamically-generated DTO and ActionScript DTO.

Implementing the uid Property in a Java DTO

Assuming that the property id is the key property of your DTO, you may implement the uid property as follows:

	@Transient private String uid;
	@FXIgnore
	public String getUid() {
		if (uid == null) {
			uid = "" + id;
		}
		return uid;
	}
 
	public void setUid(String uid) {
		this.uid = uid;
	}

Note that for a JPA entity, as oppose to the plain DTO, the corresponding id property would be annotated as @Id.

Implementing uid in the ActionScript DTO

CDB automatically implements uid in the generated ActionScript DTO as long as you annotate some property in a Java DTO with @Id and/or @FXKeyColumn.

Note. If the property is implemented with the get()/set() method pair, annotation @FXKeyColumn should be placed in front of the getter. This holds true even if the underlying Java variable is defined as public. However, if the property is implemented by the public variable alone, the annotation @FXKeyColumn has to be placed in front of the variable declaration:

.  .  .
@Id
private Integer id;
.  .  .
@FXKeyColumn
public Integer getId() {
    return this.id;
}

If we consider Company.java prepared in JPA Entities From Tables Generation - as sample DTO - the annotated class should look the following way:

package com.farata.test.entity;
import java.io.Serializable;
import javax.persistence.*;
import com.farata.dto2fx.annotations.FXClass;
import com.farata.dto2fx.annotations.FXClassKind;
import com.farata.dto2fx.annotations.FXIgnore;
import com.farata.dto2fx.annotations.FXKeyColumn;
import java.util.Set;
 
/**
 * The persistent class for the company database table.
 * 
 */
@Entity
@Table(name="company")
@FXClass(kind=FXClassKind.REMOTE)
public class Company implements Serializable {
	private static final long serialVersionUID = 1L;
 
	@Transient private String uid;
	@FXIgnore
	public String getUid() {
		if (uid == null) {
			uid = "" + id;
		}
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}	
 
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Integer id;
 
	@Column(name="company_name")
	private String companyName;
 
	//bi-directional many-to-one association to CompanyAssociate
	@OneToMany(mappedBy="company")
	private Set<CompanyAssociate> companyAssociates;
 
    public Company() {}
 
    	@FXKeyColumn
	public Integer getId() {
		return this.id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
 
	public String getCompanyName() {
		return this.companyName;
	}
	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}
 
	public Set<CompanyAssociate> getCompanyAssociates() {
		return this.companyAssociates;
	}
 
	public void setCompanyAssociates(Set<CompanyAssociate> companyAssociates) {
		this.companyAssociates = companyAssociates;
	}
}	
clearwiki/20.cleardatabuilder/03.authoring_java_dto/04.maintaining_dto_identities_with_fxkey_and_uid_property.txt · Last modified: 2011/07/13 12:29 (external edit)
Back to top
CC Attribution-Noncommercial-Share Alike 3.0 Unported
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0