如何在java中将arraylist传递给数据库

时间:2011-07-25 10:30:31

标签: java xml arraylist

我用hibernate创建了一个新的derby数据库。我能够在Apache derby中创建一个数据库。我能够从xml中的节点获取数据。并将其存储在arraylist中。我知道如何通过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

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


       }

我需要将arraylist值传递给设备类。任何人都可以帮我如何将值传递给设备类

1 个答案:

答案 0 :(得分:1)

如果使用集合类型(例如,在您的情况下为Set),则必须指定此集合如何映射到另一个数据库表。因此,在您的对象中,它是一个可直接访问的子对象集合,但在您的数据库中,它仍然分为2个具有1:n或n:m关系的表。

如果您使用JPA,只需在注释中指定关系:

@OneToMany(mappedBy = "sale")
private List<Sale> saleList;

在JPA 2.0中,您甚至可以避免在仅使用基本类型时指定单独的实体:

@ElementCollection@CollectionTable(name="item")
@Column(name="name")
private List<String> items;