Playframework b=(EntityBean)bean; EntityBeanIntercept ebi=eb._ebean_getIntercept(); //这是简单的情况吗? 如果(!ebi.isLoadedProperty(property.getPropertyIndex())返回false; //如果是列表,请检查是否已填充 对象值=property.getValue(eb); if(BeanList的值实例){ BeanList=(BeanList)值; //如果标记为已加载且不为空 返回列表。isPopulated(); } } 返回true; }

Playframework b=(EntityBean)bean; EntityBeanIntercept ebi=eb._ebean_getIntercept(); //这是简单的情况吗? 如果(!ebi.isLoadedProperty(property.getPropertyIndex())返回false; //如果是列表,请检查是否已填充 对象值=property.getValue(eb); if(BeanList的值实例){ BeanList=(BeanList)值; //如果标记为已加载且不为空 返回列表。isPopulated(); } } 返回true; },playframework,jackson,ebean,Playframework,Jackson,Ebean,然后在ebean模型类上,如下所示使用它: @JsonIgnore @ManyToMany(fetch=LAZY) @JoinTable(...) public List<RelatedEntity> relatedEntities; // getter used during JSON serialize to avoid ebean lazy loading @JsonProperty("relatedEntities") @JsonInclude(JsonInclude.In

然后在ebean模型类上,如下所示使用它:

@JsonIgnore
@ManyToMany(fetch=LAZY)
@JoinTable(...)
public List<RelatedEntity> relatedEntities;

// getter used during JSON serialize to avoid ebean lazy loading
@JsonProperty("relatedEntities")
@JsonInclude(JsonInclude.Include.NON_NULL)
public List<RelatedEntity> getJsonRelatedEntities() {
    if (Helper.isPropertyLoaded(ThisEntity.class, this, "relatedEntities")) {
        return relatedEntities;
    } else {
        return null;
    }
}
@JsonIgnore
@ManyToMany(fetch=LAZY)
@可接合(…)
公开列出相关实体;
//JSON序列化期间使用的getter可避免延迟加载
@JsonProperty(“相关实体”)
@JsonInclude(JsonInclude.Include.NON_NULL)
公共列表getJsonRelatedEntities(){
if(Helper.isPropertyLoaded(ThisEntity.class,这是“relatedEntities”)){
返回相关实体;
}否则{
返回null;
}
}

我需要一个解决方案来避免ebean在Json.toJson序列化过程中延迟加载,并提出了这个解决方案。它非常复杂,但它的优点是本地化到模型类,并且可以很好地使用标准的Json.toJson

定义通用帮助器:

/**
 * Check if a lazy-loaded property on an ebean model class has been loaded without accidentally loading it
 * @param clazz The class instance of the object to check (T.class)
 * @param bean The actual object to check the property on
 * @param propertyName The name of the property to check
 * @param <T> The class of the object
 * @return Whether it has already been loaded or not
 */
public static <T> boolean isPropertyLoaded(Class<T> clazz, T bean, String propertyName) {
    // avoid ebean lazy loading by using the secret ebean api
    if (bean instanceof EntityBean) {
        // obtain the ebean descriptor of the class and property
        BeanDescriptor<T> beanDescriptor = ((SpiEbeanServer) Oracle.getCurrent()).getBeanDescriptor(clazz);
        BeanProperty property = beanDescriptor.getBeanProperty(propertyName);
        EntityBean eb = (EntityBean) bean;
        EntityBeanIntercept ebi = eb._ebean_getIntercept();
        // is this the simple case?
        if (!ebi.isLoadedProperty(property.getPropertyIndex())) return false;
        // if it is a list, check if it is populated
        Object value = property.getValue(eb);
        if (value instanceof BeanList) {
            BeanList list = (BeanList) value;
            // if it is marked as loaded and is not null
            return list.isPopulated();
        }
    }
    return true;
}
/**
*检查是否在未意外加载的情况下加载了ebean模型类上的延迟加载属性
*@param clazz要检查的对象的类实例(T.class)
*@param bean检查属性的实际对象
*@param propertyName要检查的属性的名称
*@param对象的类
*@return是否已加载
*/
公共静态布尔值isPropertyLoaded(类clazz、T bean、字符串propertyName){
//通过使用秘密的ebeanapi避免延迟加载
if(EntityBean的bean实例){
//获取类和属性的ebean描述符
BeanDescriptor BeanDescriptor=((SpiEbeanServer)Oracle.getCurrent()).getBeanDescriptor(clazz);
BeanProperty属性=beanDescriptor.getBeanProperty(propertyName);
EntityBean eb=(EntityBean)bean;
EntityBeanIntercept ebi=eb._ebean_getIntercept();
//这是简单的情况吗?
如果(!ebi.isLoadedProperty(property.getPropertyIndex())返回false;
//如果是列表,请检查是否已填充
对象值=property.getValue(eb);
if(BeanList的值实例){
BeanList=(BeanList)值;
//如果标记为已加载且不为空
返回列表。isPopulated();
}
}
返回true;
}
然后在ebean模型类上,如下所示使用它:

@JsonIgnore
@ManyToMany(fetch=LAZY)
@JoinTable(...)
public List<RelatedEntity> relatedEntities;

// getter used during JSON serialize to avoid ebean lazy loading
@JsonProperty("relatedEntities")
@JsonInclude(JsonInclude.Include.NON_NULL)
public List<RelatedEntity> getJsonRelatedEntities() {
    if (Helper.isPropertyLoaded(ThisEntity.class, this, "relatedEntities")) {
        return relatedEntities;
    } else {
        return null;
    }
}
@JsonIgnore
@ManyToMany(fetch=LAZY)
@可接合(…)
公开列出相关实体;
//JSON序列化期间使用的getter可避免延迟加载
@JsonProperty(“相关实体”)
@JsonInclude(JsonInclude.Include.NON_NULL)
公共列表getJsonRelatedEntities(){
if(Helper.isPropertyLoaded(ThisEntity.class,这是“relatedEntities”)){
返回相关实体;
}否则{
返回null;
}
}

我试图找到更简单的方法。。。可以简单忽略懒惰代理的东西。。。。我将坚持使用Hibernate解决方案,非常感谢Rob!好啊如果您开始使用二级缓存(可能已经部分加载了数据),这将不会太好。。。当然,我想找一个更简单的方法。。。可以简单忽略懒惰代理的东西。。。。我将坚持使用Hibernate解决方案,非常感谢Rob!好啊如果您开始使用二级缓存(可能已经部分加载了数据),这将不会太好。。。当然可以。所以你可能在找:。。。但请再次注意,如果同时合并了二级缓存,那么“是否加载了此属性”可能不会像您预期的那样工作。所以一般来说,我推荐一种更明确的方法。因此,您可能正在寻找:。。。但请再次注意,如果同时合并了二级缓存,那么“是否加载了此属性”可能不会像您预期的那样工作。所以一般来说,我会推荐一种更明确的方法。请注意,事实并非如此,Ebean肯定存在一些不错的选择。一旦你需要考虑部分对象和二级缓存,这个问题就更有趣了,你需要更多地考虑它。请注意,事实并非如此,Ebean肯定存在一些不错的选择。一旦您需要考虑部分对象和二级缓存,这个问题就更有趣了,您需要更多地考虑它。
query.setDisableLazyLoading(true)
/**
 * Check if a lazy-loaded property on an ebean model class has been loaded without accidentally loading it
 * @param clazz The class instance of the object to check (T.class)
 * @param bean The actual object to check the property on
 * @param propertyName The name of the property to check
 * @param <T> The class of the object
 * @return Whether it has already been loaded or not
 */
public static <T> boolean isPropertyLoaded(Class<T> clazz, T bean, String propertyName) {
    // avoid ebean lazy loading by using the secret ebean api
    if (bean instanceof EntityBean) {
        // obtain the ebean descriptor of the class and property
        BeanDescriptor<T> beanDescriptor = ((SpiEbeanServer) Oracle.getCurrent()).getBeanDescriptor(clazz);
        BeanProperty property = beanDescriptor.getBeanProperty(propertyName);
        EntityBean eb = (EntityBean) bean;
        EntityBeanIntercept ebi = eb._ebean_getIntercept();
        // is this the simple case?
        if (!ebi.isLoadedProperty(property.getPropertyIndex())) return false;
        // if it is a list, check if it is populated
        Object value = property.getValue(eb);
        if (value instanceof BeanList) {
            BeanList list = (BeanList) value;
            // if it is marked as loaded and is not null
            return list.isPopulated();
        }
    }
    return true;
}
@JsonIgnore
@ManyToMany(fetch=LAZY)
@JoinTable(...)
public List<RelatedEntity> relatedEntities;

// getter used during JSON serialize to avoid ebean lazy loading
@JsonProperty("relatedEntities")
@JsonInclude(JsonInclude.Include.NON_NULL)
public List<RelatedEntity> getJsonRelatedEntities() {
    if (Helper.isPropertyLoaded(ThisEntity.class, this, "relatedEntities")) {
        return relatedEntities;
    } else {
        return null;
    }
}