为什么Java值对象作为Generic Flex对象返回?

时间:2011-08-04 15:18:19

标签: java flex hibernate actionscript cairngorm

我会尽力提供尽可能详细的信息。

我正在使用Flex(Cairngorm 2)/ Java / Hibernate创建CRM应用程序。 我遇到的基本问题是:

我在Java中有一个Customer类,它有一个AddressList的ArrayList。 我在Flex中有一个Customer类,它有一个AddressCollection的Address类。

当我为Customer调用远程对象时,我在Flex中返回一个Customer对象,但ArrayCollecion对象的数据类型为Object而不是Address。

如果我尝试调用Address类的List,我会得到相同的结果。 如果我尝试调用Customer类列表,我会在Flex中获得一个Customer类列表。

将tomcat 6与以下罐子一起使用:

antlr-2.7.6.jar               
flex-messaging-proxy.jar
backport-util-concurrent.jar  
flex-messaging-remoting.jar
cfgatewayadapter.jar          
hibernate3.jar
commons-codec-1.3.jar         
hibernate-jpa-2.0-api-1.0.0.Final.jar
commons-collections-3.1.jar   
javassist-3.12.0.GA.jar
commons-httpclient-3.0.1.jar  
jta-1.1.jar
commons-logging.jar           
logback-classic-0.9.28.jar
concurrent.jar                
logback-core-0.9.28.jar
dom4j-1.6.1.jar               
mybatis-3.0.4.jar
flex-messaging-common.jar     
mysql-connector-java-5.1.16-bin.jar
flex-messaging-core.jar       
slf4j-api-1.6.1.jar
flex-messaging-opt.jar        
xalan.jar

Customer.as

package com.middleburgsalesandservice.gryphon.vo.customer

{


import com.middleburgsalesandservice.gryphon.vo.common.AttributeCollection;



import mx.collections.ArrayCollection;



[Bindable]

[RemoteClass(alias="com.middleburgsalesandservice.gryphon.pojo.customer.Customer")]

public class Customer

{

    public var customerId:int;

    public var startDate:Date = new Date();

    public var endDate:Date = new Date();

    public var createdDate:Date = new Date();

    public var customerFirstName:String;            

    public var customerLastName:String;         

    public var customerUserName:String;     

    public var customerPassWord:String;

    public var addresses:ArrayCollection = new ArrayCollection();

    public var phones:ArrayCollection = new ArrayCollection();

    public var customerOrders:ArrayCollection = new ArrayCollection();

    public var webSites:ArrayCollection = new ArrayCollection();

    public var emails:ArrayCollection = new ArrayCollection();

    public var attributes:ArrayCollection = new ArrayCollection();

    public var attributeCollection:AttributeCollection = new AttributeCollection();



    public function Customer()

    {

        super();

    }



}

}

Address.as

package com.middleburgsalesandservice.gryphon.vo.common

{



[Bindable]

[RemoteClass(alias="com.middleburgsalesandservice.gryphon.pojo.common.Address")]

public class Address 

{

    public var addressId:int;

    public var address1:String;

    public var address2:String; 

    public var city:String;

    public var state:State = new State();

    public var zipCode:String;

    public var addressType:AddressType = new AddressType();

    public var country:Country = new Country();

    public var primaryNumber:int;

    public var primaryAddress:Boolean;

    public var startDate:Date;

    public var endDate:Date;



    public function Address()

    {

        super();

    }



}

}

Customer.java

package com.middleburgsalesandservice.gryphon.pojo.customer;



 import java.util.ArrayList;

 import java.util.Date;

 import java.util.HashSet;

 import java.util.List;

 import java.util.Set;



 import com.middleburgsalesandservice.gryphon.pojo.common.Address;

 import com.middleburgsalesandservice.gryphon.pojo.common.Attribute;

 import com.middleburgsalesandservice.gryphon.pojo.common.AttributeCollection;

 import com.middleburgsalesandservice.gryphon.pojo.common.EmailAddress;

 import com.middleburgsalesandservice.gryphon.pojo.common.Phone;

 import com.middleburgsalesandservice.gryphon.pojo.common.WebAddress;

 import com.middleburgsalesandservice.gryphon.pojo.order.Order;




 public class Customer {

private int customerId;

private Date startDate;

private Date endDate;

private String customerFirstName;           

private String customerLastName;            

private Date createdDate;

private String customerUserName;        

private String customerPassWord;

private List<Address> addresses = new ArrayList<Address>();

private List<Phone> phones = new ArrayList<Phone>();

private List<Order> customerOrders = new ArrayList<Order>();

private List<WebAddress> webSites = new ArrayList<WebAddress>();

private List<EmailAddress> emails = new ArrayList<EmailAddress>();

private List<Attribute> attributes = new ArrayList<Attribute>();

private AttributeCollection attributeCollection;

public String getCustomerFirstName() {

    return customerFirstName;

}

public void setCustomerFirstName(String customerFirstName) {

    this.customerFirstName = customerFirstName;

}

public String getCustomerLastName() {

    return customerLastName;

}

public void setCustomerLastName(String customerLastName) {

    this.customerLastName = customerLastName;

}

public String getCustomerUserName() {

    return customerUserName;

}

public void setCustomerUserName(String customerUserName) {

    this.customerUserName = customerUserName;

}

public String getCustomerPassWord() {

    return customerPassWord;

}

public void setCustomerPassWord(String customerPassWord) {

    this.customerPassWord = customerPassWord;

}

public List<Address> getAddresses() {

    return addresses;

}

public void setAddresses(List<Address> addresses) {

    this.addresses = addresses;

}

public List<Phone> getPhones() {

    return phones;

}

public void setPhones(List<Phone> phones) {

    this.phones = phones;

}

public String getFullName(){

    return this.getCustomerFirstName() + " " + this.getCustomerLastName();

}

/**

 * @return the createdDate

 */

public Date getCreatedDate() {

    return createdDate;

}

/**

 * @param createdDate the createdDate to set

 */

public void setCreatedDate(Date createdDate) {

    this.createdDate = createdDate;

}

/**

 * @return the customerOrders

 */

public List<Order> getCustomerOrders() {

    return customerOrders;

}

/**

 * @param customerOrders the customerOrders to set

 */

public void setCustomerOrders(List<Order> customerOrders) {

    this.customerOrders = customerOrders;

}

/**

 * @return the webSites

 */

public List<WebAddress> getWebSites() {

    return webSites;

}

/**

 * @param webSites the webSites to set

 */

public void setWebSites(List<WebAddress> webSites) {

    this.webSites = webSites;

}

/**

 * @return the emails

 */

public List<EmailAddress> getEmails() {

    return emails;

}

/**

 * @param emails the emails to set

 */

public void setEmails(List<EmailAddress> emails) {

    this.emails = emails;

}

/**

 * @return the attributeCollection

 */

public AttributeCollection getAttributeCollection() {

    return attributeCollection;

}

/**

 * @param attributeCollection the attributeCollection to set

 */

public void setAttributeCollection(AttributeCollection attributeCollection) {

    this.attributeCollection = attributeCollection;

}

/**

 * @return the attributes

 */

public List<Attribute> getAttributes() {

    return attributes;

}

/**

 * @param attributes the attributes to set

 */

public void setAttributes(List<Attribute> attributes) {

    this.attributes = attributes;

}

/**

 * @return the customerId

 */

public int getCustomerId() {

    return customerId;

}

/**

 * @param customerId the customerId to set

 */

public void setCustomerId(int customerId) {

    this.customerId = customerId;

}

/**

 * @return the startDate

 */

public Date getStartDate() {

    return startDate;

}

/**

 * @param startDate the startDate to set

 */

public void setStartDate(Date startDate) {

    this.startDate = startDate;

}

/**

 * @return the endDate

 */

public Date getEndDate() {

    return endDate;

}

/**

 * @param endDate the endDate to set

 */

public void setEndDate(Date endDate) {

    this.endDate = endDate;

}

/* (non-Javadoc)

 * @see java.lang.Object#toString()

 */

@Override

public String toString() {

    return "ID=" + this.getCustomerId() + "|FirstName=" + this.getCustomerFirstName() + "|LastName=" + this.getCustomerLastName();

}

}

Address.java

 package com.middleburgsalesandservice.gryphon.pojo.common;



import java.util.Date;





public class Address {

private int addressId;

private Date startDate;

private Date endDate;

private String address1;

private String address2; 

private String city;

private State state;

private String zipCode;

private AddressType addressType;

private Country country;

private boolean primaryAddress;

public Country getCountry() {

    return country;

}

public void setCountry(Country country) {

    this.country = country;

}

public AddressType getAddressType() {

    return addressType;

}

public void setAddressType(AddressType addressType) {

    this.addressType = addressType;

}

public String getAddress1() {

    return address1;

}

public void setAddress1(String address1) {

    this.address1 = address1;

}

public String getAddress2() {

    return address2;

}

public void setAddress2(String address2) {

    this.address2 = address2;

}

public String getCity() {

    return city;

}

public void setCity(String city) {

    this.city = city;

}

public State getState() {

    return state;

}

public void setState(State state) {

    this.state = state;

}

public String getZipCode() {

    return zipCode;

}

public void setZipCode(String zipCode) {

    this.zipCode = zipCode;

}

/**

 * @return the primaryAddress

 */

public boolean isPrimaryAddress() {

    return primaryAddress;

}

/**

 * @param primaryAddress the primaryAddress to set

 */

public void setPrimaryAddress(boolean primaryAddress) {

    this.primaryAddress = primaryAddress;

}

/**

 * @return the addressId

 */

public int getAddressId() {

    return addressId;

}

/**

 * @param addressId the addressId to set

 */

public void setAddressId(int addressId) {

    this.addressId = addressId;

}

/**

 * @return the endDate

 */

public Date getEndDate() {

    return endDate;

}

/**

 * @param endDate the endDate to set

 */

public void setEndDate(Date endDate) {

    this.endDate = endDate;

}

/**

 * @return the startDate

 */

public Date getStartDate() {

    return startDate;

}

/**

 * @param startDate the startDate to set

 */

public void setStartDate(Date startDate) {

    this.startDate = startDate;

}

}

Hibernate映射: Customer.hbm.xml

  <?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>

    <class name="com.middleburgsalesandservice.gryphon.pojo.customer.Customer" table="customer">

        <id column="customer_id" name="customerId" type="int">

            <generator class="native"/>

        </id>

        <property column="cust_f_name" name="customerFirstName" />

        <property column="cust_l_name" name="customerLastName" />

        <property column="cust_user_name" name="customerUserName" />

        <property column="cust_pass_word" name="customerPassWord" />

        <property column="startdate" name="startDate" type="java.util.Date"/>

        <property column="enddate" name="endDate" type="java.util.Date"/>

        <property column="created_dt" name="createdDate" type="java.util.Date"/>

        <many-to-one column="attribute_col_id" name="attributeCollection" class="com.middleburgsalesandservice.gryphon.pojo.common.AttributeCollection"/>

        <bag name="addresses" table="cust_address" lazy="false">

            <key column="customer_id"/>

            <many-to-many column="address_id" class="com.middleburgsalesandservice.gryphon.pojo.common.Address"/>

        </bag>

        <bag name="phones" table="cust_phone" lazy="false">

            <key column="customer_id"/>

            <many-to-many column="phone_id" class="com.middleburgsalesandservice.gryphon.pojo.common.Phone"/>

        </bag>

        <bag name="webSites" table="cust_web_address" lazy="false">

            <key column="customer_id"/>

            <many-to-many column="web_address_id" class="com.middleburgsalesandservice.gryphon.pojo.common.WebAddress"/>

        </bag>

        <bag name="emails" table="cust_email" lazy="false">

            <key column="customer_id"/>

            <many-to-many column="email_id" class="com.middleburgsalesandservice.gryphon.pojo.common.EmailAddress"/>

        </bag>

        <bag name="attributes" table="cust_attributes" lazy="false">

            <key column="customer_id"/>

            <many-to-many column="attribute_id" class="com.middleburgsalesandservice.gryphon.pojo.common.Attribute"/>

        </bag>

        <bag name="customerOrders" table="customer_orders" lazy="false">

            <key column="customer_id"/>

            <many-to-many column="order_id" class="com.middleburgsalesandservice.gryphon.pojo.order.Order"/>

        </bag>

     </class>

   </hibernate-mapping>

Address.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">
<hibernate-mapping>
  <class name="com.middleburgsalesandservice.gryphon.pojo.common.Address" table="address">
        <id column="address_id" name="addressId" type="int">
            <generator class="native"/>
        </id>
        <property column="startdate" name="startDate" type="java.util.Date"/>
        <property column="enddate" name="endDate" type="java.util.Date"/>
        <property column ="addr1" name="address1" />
        <property column ="addr2" name="address2" />
        <property column="city" name="city" />
        <property column="zip_code" name="zipCode" />
        <many-to-one column="state_id" name="state" class="com.middleburgsalesandservice.gryphon.pojo.common.State" />      
        <many-to-one column="address_type_id" name="addressType" class="com.middleburgsalesandservice.gryphon.pojo.common.AddressType" />
        <many-to-one column="country_id" name="country" class="com.middleburgsalesandservice.gryphon.pojo.common.Country" />
        <property column="primary_address" name="primaryAddress" type="java.lang.Boolean" />
  </class>
  </hibernate-mapping>

1 个答案:

答案 0 :(得分:4)

您应该在SWF中的某个地方引用您的Address课程。最好的方法是在主应用程序中声明一个字段:

private var references:Object = {addressClass:Address, someOtheClass:SomeOtherClass};

问题是ActionScript中的ArrayCollection没有类型引用(Java中的泛型)。所以类'字节码不包含在SWF中。