2012-06-12 32 views
-2

如何加入newMap detals 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; 
} 

} 

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

    } 
+1

你所說的'join'意思?你的示例代碼有什麼問題?它是做你想做的嗎? –

+0

我想遷移newMap的值到custMap.i我得到的錯誤是方法put(字符串,客戶)在類型地圖<字符串,客戶>不適用 爲參數(字符串,DoCustomer) – nag

+0

您可以張貼Customer和DoCustomer類的定義? – tibtof

回答

2

你的意思是:

custMap.putAll(newMap) 
+1

-1這不起作用。 「DoCustomer」不是「客戶」。 – Kai

+0

雅我想要這個溶膠,但我得到錯誤是方法把(字符串,客戶)在類型映射<字符串,客戶>不適用 爲參數(字符串,DoCustomer) – nag

+0

從類'DoCustomer'我的名字假設它是'Customer'類的一個子類,但是我犯了一個錯誤。所以我提出的方法並不適合你的情況 – maks

4

與您的代碼的問題是,你想提出一個DoCustomerCustomer容器。它只適用於DoCustomerCustomer的子類。

編輯1:您可以使用BeanUtilsDoCustomer轉換爲CustomerHere是一個很好的教程。

+0

謝謝DoCustomer不是客戶的子類,在這個例子中,客戶是實體,DoCustomer是客戶的特性,你能告訴我另一種方式解決這個問題嗎?可能使用BeanUtils ..... – nag

0

正如其他人所指出的,我們需要知道DoCustomer能夠提供哪些幫助。

但是,根據您給我們的建議,我建議將每個DoCustomer投射到客戶,或者更準確地說,從每個DoCustomer的字段中創建一個新客戶。

喜歡的東西:

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

您的循環內。

我可以看到你所提供沒有一個構造函數中定義的客戶類,自然你就必須添加一個到它

+0

謝謝邁克爾你寫,但在我的客戶實體中映射CustomerBranch在那段時間我是我pblm與構造器 – nag

+0

你是指Set customerBranch?這與DoCustomer中的List doCustomerBranch相同嗎?我可以理解,在兩者之間轉換會使這個解決方案非常冗長。 –

+0

好的,謝謝你,我會去替代 – nag