使用Netbeans开发嵌入式数据库应用程序(Derby)。我想弄清楚多对多的关系是如何运作的。 Netbeans为我创建了实体类。我理解如何检索记录并将新结果提交给记录,但我不熟悉如何提交“集合”,请参阅下面的代码。
@Entity
@Table(name = "MASTER")
@XmlRootElement
@NamedQueries({....
})
public class Master implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
@Column(name = "ID")
private Integer id;
@Column(name = "SDS_NUMBER")
private Integer sdsNumber;
@Column(name = "PRODUCT_NAME")
private String productName;
@Column(name = "PRODUCT_DESCRIPTION")
private String productDescription;
@Column(name = "SDS_FILE_NAME")
private String sdsFileName;
@Column(name = "USE_STATUS")
private Boolean useStatus;
@Column(name = "DATA_UPDATED")
@Temporal(TemporalType.DATE)
private Date dataUpdated;
@Column(name = "PROPER_SHIPPING_NAME")
private String properShippingName;
@Column(name = "SIGNAL_WORD")
private String signalWord;
@Column(name = "GHS_COMPLIANT")
private Boolean ghsCompliant;
@JoinTable(name = "STATEMENT_LOOKUP", joinColumns = {
@JoinColumn(name = "SDS_NUMBER", referencedColumnName = "SDS_NUMBER")},
inverseJoinColumns = {
@JoinColumn(name = "STATEMENT_ID", referencedColumnName = "STATEMENT_ID")})
@ManyToMany
private Collection<Statements> statementsCollection;
@ManyToMany(mappedBy = "masterCollection")
private Collection<Manufacturers> manufacturersCollection;
@JoinTable(name = "PICTOGRAM_LOOKUP", joinColumns = {
@JoinColumn(name = "SDS_NUMBER", referencedColumnName = "SDS_NUMBER")}, inverseJoinColumns = {
@JoinColumn(name = "PICTOGRAM_ID", referencedColumnName = "PICTOGRAM_ID")})
@ManyToMany
private Collection<Pictograms> pictogramsCollection;
@ManyToMany(mappedBy = "masterCollection")
private Collection<Locations> locationsCollection;
@ManyToMany(mappedBy = "masterCollection")
private Collection<Keywords> keywordsCollection;
public Master() {
}
public Master(Integer id) {
this.id = id;
} Getters and Setters follow.......
@XmlTransient
public Collection<Keywords> getKeywordsCollection() {
return keywordsCollection;
}
public void setKeywordsCollection(Collection<Keywords> keywordsCollection) {
this.keywordsCollection = keywordsCollection;
}
public class NewClass {
public static void main(String[] args) {
EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory("JavaApplication20PU");
EntityManager em = entityManagerFactory.createEntityManager();
EntityTransaction userTransaction = em.getTransaction();
userTransaction.begin();
Master record = new Master();
record.setProductName("Test Product Name");
record.setProductDescription("Test Product Description");
record.setProperShippingName("Proper shipping name test");
record.setSdsNumber(999);
record.setSignalWord("WARNING");
record.setUseStatus(false);
record.setGhsCompliant(false);
record.setKeywordsCollection();// This is where I need help!
em.persist(record);
userTransaction.commit();
em.close();
entityManagerFactory.close();
}
}
以上是我的测试类中填充记录的代码。注释行是我需要帮助的地方。最终,这需要从Jlist中选择,但任何通用的帮助或链接到某个地方,我可以更好地了解如何工作将不胜感激。谢谢!
答案 0 :(得分:0)