小编Man*_*rte的帖子

当repository.save时,Spring Boot + JPA + Hibernate不会提交

我有一个Spring Boot Application + JPA with MySQL.在我的控制器中,当我发布一个实体时,我可以调用repository.save,然后返回"created/updated"对象.

但是,当我查看数据库时,我发现对象没有更新.

这是我的application.yml:

spring:
  jpa:
    show-sql: true
    generate-ddl: false
    hibernate:
      ddl-auto: none
    properties:
      hibernate.dialect: org.hibernate.dialect.MySQLDialect
      org.hibernate.envers.store_data_at_delete: true
      org.hibernate.envers.global_with_modified_flag: true
      org.hibernate.envers.track_entities_changed_in_revision: true
  datasource:
    initialize: false
    url: jdbc:mysql://${DB_HOST:localhost}:${DB_PORT:3306}/${DB_NAME:databaseName}?createDatabaseIfNotExist=true
    username: ${DB_USERNAME:root}
    password: ${DB_PASSWORD:root}
    driver-class-name: com.mysql.jdbc.Driver
    hikari:
      minimumIdle: 20
      maximumPoolSize: 30
      idleTimeout: 5000
      data-source-properties:
        cachePrepStmts: true
        prepStmtCacheSize: 250
        prepStmtCacheSqlLimit: 2048
Run Code Online (Sandbox Code Playgroud)

你知道我还需要做什么吗?

这是我的控制器:

@RequestMapping(method = RequestMethod.POST, produces = "application/json")
public MyEntity saveMyEntity(@Valid @RequestBody final MyEntity myEntity) {
    Assert.notNull(myEntity, "The entry cannot be null");
    return myEntityService.save(myEntity);
}
Run Code Online (Sandbox Code Playgroud)

和MyEntityService:

@Override
@UserCanCud …
Run Code Online (Sandbox Code Playgroud)

hibernate jpa commit spring-boot

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

使用反射从泛型结构获取类型参数

想象一下我有以下结构:

type MyGeneric[T string | int] struct {
}
Run Code Online (Sandbox Code Playgroud)

我想在创建新的 MyGeneric 时检查用于实例化该结构的泛型是字符串还是 int。

myGenericString := MyGeneric[string]{}
myGenericString.canHandle("hello") -> should return true
myGenericString.canHandle(8) -> should return false

func (mG MyGeneric[T]) canHandle(value any) bool {
    // how to get what T is the same type as value
}

Run Code Online (Sandbox Code Playgroud)

generics reflection go

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

具有可变数量参数的接口的方法

我想问你是否可以在Java中在接口中声明一个方法但是,我希望定义的方法可以有可变数量的输入参数(例如,所有相同的类型).我在考虑这样的事情:

public interface EqualsCriteria {

    public boolean isEqual(String... paramsToCheck); 
    // this is not equals(Object obj) !!!

}
Run Code Online (Sandbox Code Playgroud)

并且一个类实现了相同的标准,例如:

public class CommonEquals implements EqualsCriteria {
    private String name;
    private String surname;

    ....
    @Override
    public boolean isEqual(String otherName, String otherSurname) {
        return name.equals(otherName) && surname.equals(otherSurname);
    }

}
Run Code Online (Sandbox Code Playgroud)

但也许,我想在代码的另一部分中使用其他标准,就像这样

public class SpecialEquals implements EqualsCriteria {
    ....
    @Override
    public boolean isEqual(String otherName, String otherSurname, String passport) {
        return name.equals(otherName) && surname.equals(otherSurname) && passport.equals(passport);
    }

}
Run Code Online (Sandbox Code Playgroud)

PS:其实我的问题有点复杂,但这对我有用.

java interface

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

Equals和hashCode与EqualsVerifier签订合同

我对使用EqualsVerifier库的Java 有一些疑问equalshashCode合同.

想象一下,我们有类似的东西

public abstract class Person {

    protected String name;

    @Override
    public boolean equals(Object obj) {
        // only name is taken into account
    }

    @Override
    public int hashCode() {
        // only name is taken into account
    }

}
Run Code Online (Sandbox Code Playgroud)

以下扩展课程:

public final class Worker extends Person {

    private String workDescription;

    @Override
    public final boolean equals(Object obj) {
        // name and workDescription are taken into account
    }

    @Override
    public final int hashCode() {
        // name …
Run Code Online (Sandbox Code Playgroud)

java final equals hashcode equalsverifier

6
推荐指数
2
解决办法
2987
查看次数

在没有 Spring Boot Application 主类的项目中测试 Spring Data Repository

我有一个不包含运行 Spring Boot 应用程序的类的小项目。在那个类中,我只有一些配置和一些存储库。我想在小项目中测试这些存储库。

为此,我有以下几点:

@SpringBootTest
@DataJpaTest
public class TaskRepositoryTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private TaskRepository taskRepository;

    @Test
    public void test() {
        taskRepository.save(new Task("open"));
    }
}
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误

Caused by: java.lang.NoSuchMethodError: org.springframework.boot.jdbc.DataSourceBuilder.findType(Ljava/lang/ClassLoader;)Ljava/lang/Class;
Run Code Online (Sandbox Code Playgroud)

知道我必须做什么吗?

testng spring-data-jpa spring-boot

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

Spring Boot Application to validate oauth2 token from Google

I have my Spring Boot application, that provides some rest endpoints. Those rest endpoints need security, and I want to use the Oauth2 for it.

My idea is to use Google oauth2 token for that. I don't want to provide login functionality in my Spring Boot app, so I just want to check that the Bearer token is there and get the user info from it to display his/her data accordingly.

I'm checking this tutorial, but I don't think it's …

java oauth-2.0 spring-boot spring-security-oauth2

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

将Auth0Lock更改为Auth0-js

在我的Angular4项目中,我尝试从auth0-lock迁移到auth0-js.为此我创建了AuthService类,您可以在文档中看到并安装auth0-js包

npm install --save auth0-js
Run Code Online (Sandbox Code Playgroud)

但是,当我运行我的应用程序时,我收到以下错误:

ERROR TypeError: Cannot read property 'WebAuth' of undefined
Run Code Online (Sandbox Code Playgroud)

如果我下载快速入门中可以找到的示例项目,我不会收到此错误,但我不知道还有什么我必须做的.

auth0 angular

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

Java中的泛型生产者和消费者

我有这个方法来检索作为给定类的实例的对象:

public class UtilitiesClass {

    public static final Collection<Animal> get(Collection<Animal> animals, Class<? extends Animal> clazz) {
        // returns the Animals which are an instanceof clazz in animals
    }
...
}
Run Code Online (Sandbox Code Playgroud)

要调用该方法,我可以这样做:

Collection<Animal> dogs = UtilitiesClass.get(animals, Dog.class);
Run Code Online (Sandbox Code Playgroud)

这很好,但我也希望能够通过以下两种方式调用该方法:

Collection<Animal> dogs = UtilitiesClass.get(animals, Dog.class);
Run Code Online (Sandbox Code Playgroud)

要么

Collection<Dog> dogsTyped = UtilitiesClass.get(animals, Dog.class);
Run Code Online (Sandbox Code Playgroud)

我的意思是我希望能够将方法的结果存储在Dog Collection或Animal one中,因为我需要Dog.class扩展Animal.class

我在考虑这样的事情:

public static final <T> Collection<T> get(Class<T extends Animal> clazz) {
    // returns the Animals which are an instanceof clazz
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用.任何提示?

编辑:最后,使用@Rohit Jain答案,这是调用UtilitiesClass方法时的解决方案:

Collection<? extends …
Run Code Online (Sandbox Code Playgroud)

java generics pecs

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

Stomp WebSocket 订阅两个不同的主题

我有一台 websocket 服务器和一个 websocket 客户端,都是用 Java 编写的。websocket 服务器有这样的:

@MessageMapping("/hello")
@SendTo("/topic/greetings")
public Greeting greeting(final HelloMessage message) throws Exception {
    Thread.sleep(1000); // simulated delay
    return new Greeting("Hello, " + message.getName() + "!");
}
Run Code Online (Sandbox Code Playgroud)

在 Java WebSocket 客户端中,我的 StompSessionHandler.afterConnected 中有以下内容:

session.subscribe("/topic/greetings", stompFrameHandler)
Run Code Online (Sandbox Code Playgroud)

然后,我可以通过客户端向服务器路径“hello”发送消息来在两者之间进行通信,然后由于客户端订阅了“topic/greetings”,我还可以使用我的 stompFrameHandler 来处理响应。

但我想知道客户端是否可以订阅两个不同的“频道”,所以在 StompSessionHandler.afterConnected 中是这样的:

session.subscribe("/topic/greetings", greetingsFrameHandler)
session.subscribe("/topic/farewell", farewellFrameHandler)
Run Code Online (Sandbox Code Playgroud)

因为我尝试过,只能接收主题/问候的事件,但不能接收主题/告别的事件。我不知道这是否重要,但为了触发告别事件,我对 websocket 服务器进行了休息调用:

@PostMapping(value = "/sendFarewellEvent", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@SendTo("/topic/farewell")
public Farewell farewell(@RequestBody final Farewell farewell) throws Exception {
    Thread.sleep(1000); // simulated delay
    return farewell;
}
Run Code Online (Sandbox Code Playgroud)

stomp websocket spring-boot

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

Java WebSocket 客户端 Spring 启动

我想创建一个带有连接到 Web 套接字服务器的 Web 套接字客户端的 Spring Boot 应用程序。

例如,我使用了 Spring Boot 中的入门指南。

https://spring.io/guides/gs/messaging-stomp-websocket/

在此示例中,您使用 Spring Boot 创建一个 Web 套接字服务器,并使用 JavaScript 连接到它。

我想运行该服务器并使用另一个创建 WebSocketClient 对象的 Spring Boot 应用程序连接到它。

这是我在 Spring Boot 客户端 App 中创建的 WebSocketClientConfiguration 类

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketClientConfig {

    @Bean
    public WebSocketClient webSocketClient() {
        final WebSocketClient client = new StandardWebSocketClient();

        final WebSocketStompClient stompClient = new WebSocketStompClient(client);
        stompClient.setMessageConverter(new MappingJackson2MessageConverter());

        final StompSessionHandler sessionHandler = new MyStompSessionHandler();
        stompClient.connect("ws://localhost:8080", sessionHandler);
        return client;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是在我的类 MyStompSessionHandler 中,在 handleTransportError 方法中我可以看到异常是

javax.websocket.DeploymentException: The HTTP …
Run Code Online (Sandbox Code Playgroud)

java stomp spring-boot spring-websocket

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

有可能以这种方式创建一个泛型类吗?

我想知道这个创作是否可行:

Class<String> stringClass = String.class;
MyGenericClass<stringClass> myGenericClass;
Run Code Online (Sandbox Code Playgroud)

我在我的IDE中尝试过,但是出现了编译错误.

java generics

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