我正在使用 ReverseMappingTool 为现有的数据库方案创建 JPA 源。
它工作正常,有一个例外:创建后,从一对多关系调用 get-Methods 数据时未加载。
JPA 文件如下所示:
@Entity
@Table(schema="myScheme", name="ORDER")
@IdClass(org.company.OrderId.class)
public class Order {
@OneToMany(targetEntity=org.company.Orderposition.class, mappedBy="order", cascade=CascadeType.MERGE)
private Set orderpositions = new HashSet();
public Set getOrderpositions() {
return bestellpositions;
}
public void setOrderpositions(Set orderpositions) {
this.orderpositions = orderpositions;
}
public Set getDependantPositions() {
if (getOrderpositions() != null) {
return getOrderpositions();
}
}
和...
@Entity
@Table(schema="myScheme", name="ORDERPOSITION")
@IdClass(org.company.Orderposition.Id.class)
public class Orderposition {
@ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.MERGE)
@JoinColumn(name="intid_strbestellung", nullable=false)
private Order order;
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
方法“getDependantPositions()”只是我在 JPA 类中放置的方法的一个示例。有必要这样做,所以让我们把它当作给定的。关键是,尽管数据库包含该对象的大量条目,但调用 getOrderpositions() 不会返回任何数据。映射工具没有为@OneToMany-Relationships 添加任何 FetchType ......所以我猜它的 LAZY 并调用 getOrderpositions() 应该返回所有相关值。
你能帮助我吗?
谢谢!
PS:这就是我调用 ReverseMappingTool 的方式:
Options opts = new Options();
// Output directory - All generated code and metadata will be written to the directory at this path
opts.setProperty("directory", PATH_SOURCEGENERATION);
// True to generate JPA annotations in generated java classes.
opts.setProperty("annotations", true);
// disable orm.xml generation
opts.setProperty("metadata", "none");
// The full class name of a org.apache.openjpa.jdbc.meta.ReverseCustomizer customization plugin
opts.setProperty("customizerClass", JPAPropertiesReverseCustomizer.name);
// Create built-in application identity classes if possible
opts.setProperty("useBuiltinIdentityClass", false);
// Avoid creating classes for many-many join tables
opts.setProperty("primaryKeyOnJoin", true);
// Prevent the creation of inverse 1-many/1-1 relations for every many-1/1-1 relation detected
opts.setProperty("inverseRelations", true);
opts.setProperty("innerIdentityClasses", true);