【问题标题】:How to join Maps如何加入地图
【发布时间】:2012-06-12 07:56:20
【问题描述】:

如何加入newMap详情custMap

 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;
}

    }

【问题讨论】:

  • join 是什么意思?您的示例代码有什么问题?它做你想做的事吗?
  • 我想将 newMap 值迁移到 custMap。我收到错误是 Map 类型中的 put(String, Customer) 方法不适用于参数 (String, DoCustomer)
  • 您能否发布 Customer 和 DoCustomer 类的定义?

标签: java collections map


【解决方案1】:

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

编辑 1:您可以使用 BeanUtilsDoCustomer 转换为 CustomerHere 是一个很好的教程。

【讨论】:

  • 谢谢,DoCustomer 不是 Customer 的子类,在这个例子中 Customer 是实体,DoCustomer 是 Customer 的属性,你能告诉我这个 sol 的另一种方法吗,是否可以使用 BeanUtils... ..
【解决方案2】:

你的意思是:

custMap.putAll(newMap)

【讨论】:

  • 是的,我想要这个溶胶,但我得到错误是 Map 类型中的 put(String, Customer) 方法不适用于参数 (String, DoCustomer)
  • 来自DoCustomer类的名称我以为它是Customer类的子类,但我做错了。所以我提出的方法不适合你的情况
【解决方案3】:

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

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

类似:

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

在你的 for 循环中。

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

【讨论】:

  • 谢谢你迈克尔你写的但是在我的客户实体中是由 CustomerBranch 映射的,那时我有 pblm 和 costructor
  • 你指的是Set customerBranch吗?这是与 DoCustomer 中的 List doCustomerBranch 相同的字段吗?我可以理解,在两者之间进行转换会使这成为一个非常冗长的解决方案。
  • custBranchesList 是 List 的列表类型, custBrancheChangesList 是 List 的列表类型 是否可以加入 custBranchesList.add(custBrancheChangesList ) ...?
  • 和你原来的问题是同一个问题;只有当您愿意使用相同的技术将它们从 DoCustomerBranch 转换为 CustomerBranch 时,您才能这样做。
猜你喜欢
  • 2018-04-17
  • 1970-01-01
  • 2021-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-05
  • 1970-01-01
相关资源
最近更新 更多