2011-07-25 6 views
1

私はhibernateで新しいDerbyデータベースを作成しました。Apache Derbyでデータベースを作成できました。xmlのノードからデータを取得できました。 arraylist。私はデータベースクラスにarraylist contantを渡す方法を知っています。javaのデータベースにarraylistを渡す方法

xml.java

 public class xml_read { 

    public static void main(String argv[]) { 

    try { 

    File fXmlFile = new File("c:\\testing.xml"); 
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
    Document doc = dBuilder.parse(fXmlFile); 
    doc.getDocumentElement().normalize(); 

    // System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); 
    NodeList nList = doc.getElementsByTagName("device"); 
    // System.out.println("-----------------------"); 

for (int temp = 0; temp < nList.getLength(); temp++) { 

    Node nNode = nList.item(temp);  
    if (nNode.getNodeType() == Node.ELEMENT_NODE) { 

    Element eElement = (Element) nNode; 
    ArrayList arrayList = new ArrayList(); 

    //System.out.println(arrayList); 
    String type=getTagValue("type", eElement); 
    String Name=getTagValue("name", eElement); 
    String Setup=getTagValue("setup", eElement); 
    arrayList.add(type); 
    arrayList.add(Name); 
    arrayList.add(Setup); 
    System.out.println(arrayList); 
     System.out.println("type : " + getTagValue("type",eElement)); 
     System.out.println("Name : " + getTagValue("name",eElement)); 
     System.out.println("Set up : " + getTagValue("setup",eElement)); 


    } 
    } 
     } catch (Exception e) { 
      e.printStackTrace(); 
    } 
    } 

    private static String getTagValue(String sTag, Element eElement){ 
    NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes(); 
    Node nValue = (Node) nlList.item(0); 

    return nValue.getNodeValue();  
    } 

} 

device.java

私はデバイスにArrayListの値を渡す必要があり
public class Device { 
    private String type; 
    private String setup; 
    private String name; 
    private Long deviceId; 
    private Set<CommandInfo> commandSet = new HashSet<CommandInfo>(); 

    public Device() { 

    } 

    public Device(String name, String type, String setup) { 
      System.out.println("name = "+name+" type = "+type+" setup = "+setup); 
      this.name = name; 
      this.type = type; 
      this.setup = setup; 
    } 

    /** 
    * @return the type 
    */ 
    public String getType() { 
     return type; 
    } 

    /** 
    * @param type the type to set 
    */ 
    public void setType(String type) { 
     this.type = type; 
    } 

    /** 
    * @return the setup 
    */ 
    public String getSetup() { 
     return setup; 
    } 

    /** 
    * @param setup the setup to set 
    */ 
    public void setSetup(String setup) { 
     this.setup = setup; 
    } 

    /** 
    * @return the name 
    */ 
    public String getName() { 
     return name; 
    } 

    /** 
    * @param name the name to set 
    */ 
    public void setName(String name) { 
     this.name = name; 
    } 

    /** 
    * @return the id 
    */ 
    @Id 
    @GeneratedValue 
    @Column(name = "DEVICE_ID") 
    public Long getDeviceId() { 
     return deviceId; 
    } 

    /** 
    * @param id the id to set 
    */ 
    public void setDeviceId(Long id) { 
     this.deviceId = id; 
    } 

    /** 
    * @return the commandSet 
    */ 
    @OneToMany(cascade = CascadeType.ALL) 
    @JoinTable(name = "DEVICE_COMMAND", joinColumns = { 
      @JoinColumn(name = "DEVICE_ID") }, 
      inverseJoinColumns = { @JoinColumn(name = "COMMAND_ID") 
    }) 
    public Set<CommandInfo> getCommandSet() { 
     return commandSet; 
    } 

    /** 
    * @param commandSet the commandSet to set 
    */ 
    public void setCommandSet(Set<CommandInfo> commandSet) { 
     this.commandSet = commandSet; 
    } 

    /* (non-Javadoc) 
    * @see java.lang.Object#toString() 
    */ 
    @Override 
    public String toString() { 
     StringBuilder builder = new StringBuilder(); 
     builder.append("Device [type="); 
     builder.append(type); 
     builder.append(", setup="); 
     builder.append(setup); 
     builder.append(", name="); 
     builder.append(name); 
     builder.append(", deviceId="); 
     builder.append(deviceId); 
     builder.append(", commandSet="); 
     builder.append(commandSet); 
     builder.append("]"); 
     return builder.toString(); 
    } 


     } 

class.Canいずれかは、どのようにデバイス・クラスに値を渡すために私を助けて

+0

なぜArrayListを反復処理せず、値をDeviceクラスに設定しないのですか? – BOSS

答えて

1

コレクションタイプ(例:あなたのケースにセット)を使用する場合は、このコレクションを別のデータベーステーブルにマップする方法を指定する必要があります。あなたのオブジェクトでは、サブオブジェクトの直接アクセス可能なコレクションですが、データベースでは1:nまたはn:mの関係を持つ2つのテーブルに分かれています。

あなたはJPAを使用している場合は、単に注釈関係を指定:JPA 2.0では

​​

あなたも、あなただけの基本的な型を使用する場合には別のエンティティを指定することを避けることができます。

@[email protected](name="item") 
@Column(name="name") 
private List<String> items; 
関連する問題