-2

如何newMapcustMap.

 Map<String, Customer> custMap= new HashMap<String,Customer>();   
 Map<String, DoCustomer> newMap= new HashMap<String,DoCustomer>();
     for (Map.Entry<String, DoCustomer> cust: newMap.entrySet()) {   
     custMap.put(cust.getKey(),cust.getValue()); 
 }

public class DoCustomer {
private Long id;

private String custName;

private String description;
    private String status;
    private List<DoCustomerBranch> doCustomerBranch=new ArrayList<DoCustomerBranch>
public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}
public String getCustName() {
    return custName;
}
public void setCustName(String custName) {
    this.custName = custName;
}
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
public String getStatus() {
    return status;
}
public void setStatus(String status) {
    this.status = status;
}
getter/setters of doCustomerBranch
}

  @Entity
  @Table(name = "CUSTOMER")
  public class Customer implements Serializable{

private static final long serialVersionUID = 1L;
private Long id;

private String custName;

private String description;

private String createdBy;
private Date createdOn;

private String updatedBy;
private Date updatedOn;


private Set<CustomerBranch> customerBranch=new HashSet<CustomerBranch>



@Id
@GeneratedValue(generator = "CUSTOMER_SEQ")
@SequenceGenerator(name = "CUSTOMER_SEQ", sequenceName = "CUSTOMERN_SEQ",   allocationSize = 1)
@Column(name = "ID")
public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}


@Column(name = "CUST_NAME",nullable=false)
public String getCustName() {
    return custName;
}

public void setCustName(String custName) {
    this.custName = custName;
}

@Column(name = "DESCRIPTION")
public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}


@Column(name = "CREATED_BY", length = 50)
public String getCreatedBy() {
    return createdBy;
}

public void setCreatedBy(String createdBy) {
    this.createdBy = createdBy;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "CREATED_ON")
public Date getCreatedOn() {
    return createdOn;
}

public void setCreatedOn(Date createdOn) {
    this.createdOn = createdOn;
}

@Column(name = "UPDATED_BY", length = 50)
public String getUpdatedBy() {
    return updatedBy;
}

public void setUpdatedBy(String updatedBy) {
    this.updatedBy = updatedBy;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "UPDATED_ON")
public Date getUpdatedOn() {
    return updatedOn;
}

public void setUpdatedOn(Date updatedOn) {
    this.updatedOn = updatedOn;
}
    @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.REMOVE }, fetch =         FetchType.LAZY, mappedBy = "customer")
public Set<CustomerBranch> getCustomerBranch() {
    return customerBranch;
}


public void setCustomerBranch(Set<CustomerBranch> customerBranch) {
    this.customerBranch = customerBranch;
}

 }

客户分行

@Entity
@Table(name = "CUSTOMER_BRANCH")
public class CustomerBranch implements Serializable{

   @Id
@GeneratedValue(generator = "CUSTOMER_BRANCH_SEQ")
@SequenceGenerator(name = "CUSTOMER_BRANCH_SEQ", sequenceName =   "CUSTOMER_BRANCH_SEQ", allocationSize = 1)
@Column(name = "ID")
private Long id;
private String branchName;

private String branchAddress;

private Customer customer;



public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

@Column(name = "BRANCH_NAME",nullable=false)
public String getBranchName() {
    return branchName;
}



@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "MOBEE_CUSTOMER")
public Customer getCustomer() {
    return customer;
}

public void setCustomer(Customer customer) {
    this.customer = customer;
}

    }
4

3 回答 3

4

您的代码的问题是您想将 aDoCustomer放入Customer容器中。它仅在DoCustomer是 的子类时才有效Customer

编辑 1:您可以使用BeanUtils将 aDoCustomer转换为Customer. 是一个很好的教程。

于 2012-06-12T07:59:49.317 回答
3

你的意思是:

custMap.putAll(newMap)
于 2012-06-12T07:59:06.627 回答
0

正如其他人所指出的那样,我们需要知道 DoCustomer 能够提供帮助。

但是,根据您给我们的信息,我建议将每个 DoCustomer 转换为 Customer,或者更准确地说,从每个 DoCustomer 的字段中创建一个新客户。

就像是:

custMap.put(cust.getKey(), new Customer(cust.getValue().getId(), cust.getValue().getCustName(), and so on..));

在你的 for 循环中。

我可以看到您提供的定义的客户类没有构造函数,因此您自然必须向其中添加一个

于 2012-06-12T10:04:39.360 回答