簡體   English   中英

如何為多個數據庫表創建表單?

[英]How to create forms for multiple database tables?

我有一個處理文本文件的應用程序。 根據文件生成器,它會搜索一些已定義的文本。 然后,它對找到的文本執行不同的操作。 這些操作存儲在數據庫中,並且與以下實體相對應:

@Entity
@Table(name = "file_generator")
public class FileGenerator {

    @Id
    @GeneratedValue
    private long id;

    private String name;

    @OneToMany(mappedBy = "fileGenerator", cascade = CascadeType.ALL)
    private Set<Action> actions = new HashSet<>();

}

@Entity
@Table(name = "detection_text")
public class DetectionText {

    @Id
    @GeneratedValue
    private long id;

    private String text;

}

@Entity
public class Action {

    @Id
    @GeneratedValue
    private long id;

    @Enumerated(EnumType.STRING)
    private Name name;

    @ManyToOne
    @JoinColumn(name = "file_generator_id")
    private FileGenerator fileGenerator;

    @ManyToOne
    @JoinColumn(name = "detection_text_id")
    private DetectionText detectionText;

    public enum Name {

        DELETE, REPLACE_WITH_DEFAULT

    }

}

例如,如果要刪除bar生成的文件中所有出現的foo ,我將在數據庫中寫入以下內容:

file_generator:

+----+------+
| id | name |
+----+------+
|  1 | bar  |
+----+------+

detection_text:

+----+------+
| id | text |
+----+------+
|  1 | foo  |
+----+------+

行動:

+----+-------------------+-------------------+--------+
| id | file_generator_id | detection_text_id |  name  |
+----+-------------------+-------------------+--------+
|  1 |                 1 |                 1 | DELETE |
+----+-------------------+-------------------+--------+

現在,我要創建一個網站,該網站允許用戶與Action關系一起存儲新的FileGenerator ,以及一個網站,允許用戶編輯此“視圖”。 我在下面創建了一個顯示表單的代碼段。

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
<form style="margin: 20px;">
  <div class="form-group">
    <label for="file-generator-name">File generator</label>
    <input type="text" class="form-control" id="file-generator-name" name="file-generator-name" />
  </div>
  <div class="form-group">
    <label for="delete-detection-texts">'DELETE' detection texts</label>
    <select class="form-control" id="delete-detection-texts" name="delete-detection-texts" multiple="true">
      <option value="1">All</option>
      <option value="1">available</option>
      <option value="1">detection</option>
      <option value="1">texts</option>
    </select>
  </div>
  <div class="form-group">
    <label for="replace-detection-texts">'REPLACE_WITH_DEFAULT' detection texts</label>
    <select class="form-control" id="replace-detection-texts" name="replace-detection-texts" multiple="true">
      <option value="1">All</option>
      <option value="1">available</option>
      <option value="1">detection</option>
      <option value="1">texts</option>
    </select>
  </div>
  <button type="submit" class="btn btn-default">Submit</button>
</form>

如何實現此功能? 可能嗎 有更好的方法嗎?

我創建了一個支持表單的新bean:

public class FileGeneratorAndDetectionTexts {

    private String fileGeneratorName;

    private Set<DetectionText> deleteTexts;

    private Set<DetectionText> replaceTexts;

}

我將服務層中的該bean轉換為OP中的相應對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM