小编Ste*_*ood的帖子

在Spring中配置ObjectMapper

我的目标是配置objectMapper它只序列化带注释的元素@JsonProperty.

为了做到这一点,我按照这个解释说明了如何配置对象映射器.

我在这里描述了自定义objectmapper .

但是,当NumbersOfNewEvents序列化类时,它仍然包含json中的所有属性.

有人有提示吗?提前致谢

杰克逊1.8.0春季3.0.5

CustomObjectMapper

public class CompanyObjectMapper extends ObjectMapper {
    public CompanyObjectMapper() {
        super();
        setVisibilityChecker(getSerializationConfig()
                .getDefaultVisibilityChecker()
                .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)
                .withFieldVisibility(JsonAutoDetect.Visibility.NONE)
                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withIsGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withSetterVisibility(JsonAutoDetect.Visibility.DEFAULT));
    }
}
Run Code Online (Sandbox Code Playgroud)

servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="de.Company.backend.web" />

    <mvc:annotation-driven />

    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                    <property name="objectMapper" ref="jacksonObjectMapper" />
                </bean>
            </list>
        </property>
    </bean>

    <bean id="jacksonObjectMapper" class="de.Company.backend.web.CompanyObjectMapper" />
</beans>
Run Code Online (Sandbox Code Playgroud)

NumbersOfNewEvents

public class …
Run Code Online (Sandbox Code Playgroud)

java spring json jackson object-object-mapping

76
推荐指数
8
解决办法
20万
查看次数

Hibernate:OneToMany通过级联保存孩子

在Parent类中有一个列表List.保存父级时,已添加或更改的子级应由休眠保存/更新.

我已经找到了很多这方面的解释,但是,我只是没有得到它的工作.

Parent.class尝试A.

@Entity
public class Parent {
// id and other attributes
@OneToMany(mappedBy = "parent")
@org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.ALL)
protected List<child> children;
Run Code Online (Sandbox Code Playgroud)

Parent.class尝试B

@Entity
public class Parent {
// id and other attributes
  @OneToMany(mappedBy = "parent", cascade = { javax.persistence.CascadeType.ALL },
 orphanRemoval = true)
 @org.hibernate.annotations.Cascade({ 
 org.hibernate.annotations.CascadeType.PERSIST,
 org.hibernate.annotations.CascadeType.MERGE,
 org.hibernate.annotations.CascadeType.REFRESH,
 org.hibernate.annotations.CascadeType.SAVE_UPDATE,
 org.hibernate.annotations.CascadeType.REPLICATE,
 org.hibernate.annotations.CascadeType.LOCK,
 org.hibernate.annotations.CascadeType.DETACH })
protected List<child> children;
Run Code Online (Sandbox Code Playgroud)

将子项添加到新父项.之后两者都被保存了

sessionFactory.getCurrentSession().saveOrUpdate(parent);
Run Code Online (Sandbox Code Playgroud)

但是,当刷新时,我收到以下错误:

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: de.pockettaxi.backend.model.ride.RideSingle
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:243)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:456)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:265)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:275) …
Run Code Online (Sandbox Code Playgroud)

hibernate cascade one-to-many hibernate-annotations

19
推荐指数
4
解决办法
8万
查看次数

在父函数中调用覆盖的子函数

是否可以在c ++中从父函数调用子函数.

我们举个例子:父类在函数中定义(解析)一般工作流.然后,工作流调用表示流的一部分的不同方法(parseElementA).这些函数可以被子类覆盖,如果不是标准函数,则应使用父类的一部分.

我的问题是:我创建一个子对象并执行工作流功能(解析).当在工作流函数中调用覆盖函数(parseElementA)时,它从父级而不是从子级调用函数.我怎么办呢,它调用了孩子的覆盖功能.

    class Parent {
      public:
        void parse() { parseElementA(); }
        virtual void parseElementA() { printf("parent\n"); }
    };

    class Child : public Parent {
      public: 
        void parseElementA() { printf("child\n"); }
    };

    Child child;
    child.parse();
Run Code Online (Sandbox Code Playgroud)

输出是父.我能做什么让它回归孩子.

非常感谢您的任何建议.

c++ compiler-errors syntax-error virtual-inheritance

1
推荐指数
1
解决办法
1443
查看次数