小编osc*_*car的帖子

禁用特定方法的Hibernate验证

我想在特定方法中禁用hibernate验证.例如,保存正常保存的验证并禁用它们以saveWithoutValidation方法.是否可以使用一些注释来执行此操作?

我知道可以在具有属性的配置中禁用它:

<Validation-mode> NONE </ validation-mode>
Run Code Online (Sandbox Code Playgroud)

我真的只是使用带有@Valid注释的控制器上的验证来表示错误.要插入数据库,我不需要.

我的有效解决方案

LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
...
Properties jpaProperties = new Properties();
jpaProperties.put("javax.persistence.validation.mode", "NONE");
factory.setJpaProperties(jpaProperties);
Run Code Online (Sandbox Code Playgroud)

java validation spring hibernate spring-data

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

带有 @EmbeddedId 的 StackOverflowError

我有一个主类和一个辅助类,用于在保存时存储错误。\n错误可以有多种类型,主键是错误的主类和类型。\n这是我的类的映射:

\n\n

我的初级班:

\n\n
@Entity\n@Table(name = "foo")\npublic class FooEntity implements Serializable {\n\n    private static final long serialVersionUID = 1L;\n\n    @Id\n    @GeneratedValue\n    @Column(name = "id")    \n    private Integer id;\n\n    @Column(name = "param1") \n    private String param1;\n\n    @OneToMany(fetch = FetchType.LAZY, mappedBy = "id.foo", cascade = CascadeType.ALL)  \n    List<FooErrorEntity> errors;\n    //getters&setters\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的错误类别:

\n\n
@Entity\n@Table(name = "fooerror")\npublic class FooErrorEntity implements Serializable {\n\n    private static final long serialVersionUID = 1L;\n\n    @EmbeddedId\n    private FooErrorId id;\n\n    @Column(name = "descripcion")\n    private String description;\n\n    //Getters&& setters\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的错误 …

java stack-overflow spring hibernate jpa

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

排除隐藏文件夹以生成原型

我正在使用以下命令:

mvn archetype:create-from-project -Darchetype.properties=./archetype.properties
Run Code Online (Sandbox Code Playgroud)

我想排除隐藏的目录,例如.sonar,我尝试以下选项:

excludePatterns=**/*sonar/*
Run Code Online (Sandbox Code Playgroud)

excludePatterns=**/.sonar/*
Run Code Online (Sandbox Code Playgroud)

但无法与我一起工作,在非隐藏的目录下效果很好。如何排除隐藏目录?

maven maven-archetype

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

通过ID将默认订单的ID附加到使用Spring Data的Pageable

我正在使用spring Pageable数据和对象。当按在数据库中可以具有相同值的字段进行排序时,更改页面会检索错误的结果。

我正在尝试使用HandlerInterceptorAdapter通过id添加默认订单,如下所示:

我的拦截器:

public class OrderByIdWebArgumentResolver extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

        HandlerMethod hm= (HandlerMethod) handler;
        Method method = hm.getMethod();
        OrderById orderById = method.getAnnotation(OrderById.class);
        if (orderById != null) {
            for (MethodParameter parametro : hm.getMethodParameters()) {
                if (parametro.getGenericParameterType().equals(Pageable.class)) {
                    Map<String, String[]> parameters = request.getParameterMap();
                    String[] sortById = new String[2];
                    sortById[0] = "id";
                    sortById[0] = "desc";
                    parameters.put("sort", sortById);
                }

            }

        }

        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的控制器:

@OrderById
@RequestMapping(value = "/print", method = RequestMethod.GET)
public …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-data

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

使用bean的entityListener中的无限循环

我创建一个 EntityListener 来审核特定实体。存储状态更改的历史记录。我使用 AutowireHelper 注入 Bean。我在这里得到它https://guylabs.ch/2014/02/22/autowiring-pring-beans-in-hibernate-jpa-entity-listeners/comment-page-1/

这可以很好地注入存储库,但要调用任何存储库的任何函数,请返回 AutowireHelper.autowire 行。

这是我的代码,EntityListener 执行 AutowireHelper.autowire ,当涉及到“find”方法时返回到 autowire 行:

public class AuditListener {

    @Inject
    private UserRepository userRepository;

    @Inject
    private ConfigurationRepository configurationRepository;

    @PreUpdate
    public void preUpdate(Object object) {
        //TODO: pre update
        AutowireHelper.autowire(this, this.userRepository, this.configurationRepository);

        //I need get bbdd entities
        List<Configuracion> config = this.configurationRepository.findAll();

        Foo foo = (Foo) object;
        System.out.println("Foo updated: " + foo.getId());
    }
}
Run Code Online (Sandbox Code Playgroud)

我的错误:

无法提交 JPA 事务;嵌套异常是 javax.persistence.RollbackException:提交事务时出错 12:43:25,291 INFO [stdout] (http-/0.0.0.0:8080-1) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 12:43 :25,291 信息 [stdout] (http-/0.0.0.0:8080-1) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) …

java spring hibernate jpa listener

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