我决定尝试Spring,通过springinitializr(maven,java)创建了一个项目,在intellij(社区)中打开它,最后它拒绝识别它,依赖项在xml文件中以红色突出显示(它找不到它们) )以及主文件(.java,在 spring 项目中默认创建)中的所有导入和注释:@SpringBootApplication、@Springapplication 等。我也是,我一整天都在绞尽脑汁,我仍然没有找到解决方案,也许我看起来很糟糕,问题是废话,idk,所以你就不能写一个解决这个问题的解决方案吗?我怎样才能在社区版本中打开一个spring项目,也许我应该使用旗舰版。如果您需要附上屏幕截图,请写下来。
我需要使用 Hibernate、Spring 和 Lombok 制作一个空白项目。我做到了,但决定通过添加一些逻辑来进行实验。为 SessionFactory 添加 bean 后,项目停止启动并出现错误
描述:组件需要一个名为“entityManagerFactory”的 bean,但无法找到。操作:考虑在您的配置中定义一个名为“entityManagerFactory”的 bean。
请告诉我可能出了什么问题?
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>VTBInnotehDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>VTBInnotehDemo</name>
<description>Demo project for VTB Innoteh</description>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2023.0.1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
@Configuration
public class MyConfig {
@Bean
public SessionFactory getSessionFactory() {
return buildSessionFactory(Person.class, Phone.class);
}
private SessionFactory buildSessionFactory(Class<?> ...annotatedClasses) {
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
MetadataSources metadataSources = new MetadataSources(createServiceRegistry(configuration));
Arrays.stream(annotatedClasses).forEach(metadataSources::addAnnotatedClass);
Metadata metadata = metadataSources.getMetadataBuilder().build();
return metadata.getSessionFactoryBuilder().build();
}
private StandardServiceRegistry createServiceRegistry(Configuration configuration){
return new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
}
}
尽管该方法deleteById
Spring Data Jpa
确实有效,但它不会从数据库中删除该对象。我读到这是因为该对象具有关系ManyToOne
并且对其的引用保留在父实体中,该实体存储这些对象的列表,首先您需要从该列表中删除该对象。但是如何才能做到这一点,才能不加载整个对象列表(其中可能有数千个)?
我翻遍了PgAdmin
(图形数据库管理界面PostgreSQL
):在那里我没有找到entity1_entity2
主表之间的任何关系表(如)。
CascadeType
由于某种原因,它对我也不起作用。
确切地说,不可能删除与实体和Comment
相关的对象。ManyToOne
Person
Task
这是两个父实体Person
和Task
:
@Entity
@Table(name = "people")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
private String name;
@Email
private String email;
@Size(min = 5, message = "Пароль должен содержать минимум 5 символов")
private String password;
private transient String passwordConfirm;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "author")
private List<Task> createdTasks;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "executor")
private List<Task> executableTasks;
@CreatedDate
private LocalDateTime registrationDate;
public Person(){}
// getters, setters
@Entity
@Table(name = "tasks")
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank(message = "Заголовок не может быть пустым")
private String title;
@NotBlank(message = "Описание не может быть пустым")
private String description;
@NotNull(message = "У задачи должен быть статус")
@Enumerated(EnumType.STRING)
private Status status;
@NotNull(message = "У задачи должен быть приоритет")
@Enumerated(EnumType.STRING)
private Priority priority;
@ManyToOne(cascade = CascadeType.REMOVE)
@JoinColumn(name = "author_id", referencedColumnName = "id")
private Person author;
@ManyToOne(cascade = CascadeType.REMOVE)
@JoinColumn(name = "executor_id", referencedColumnName = "id")
private Person executor;
@NotNull(message = "У задачи должен быть исполнитель")
private transient long executorId;
@OneToMany(mappedBy = "task")
private List<Comment> comments;
@CreatedDate
private LocalDateTime createdAt;
private LocalDateTime editedAt;
public Task(){}
// getters, setters
子实体Comment
:
@Entity
@Table(name = "comments")
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank(message = "Комментарий не может быть пустым")
private String body;
@ManyToOne(cascade = CascadeType.REMOVE)
@JoinColumn(name = "task_id", referencedColumnName = "id")
private Task task;
private transient long taskId;
@ManyToOne(cascade = CascadeType.REMOVE)
@JoinColumn(name = "author_id", referencedColumnName = "id")
private Person author;
@CreatedDate
private LocalDateTime createdAt;
private LocalDateTime editedAt;
public Comment(){}
// getters, setters
控制器方法:
@DeleteMapping("/comment/delete/confirm/{id}")
public String deleteComment(@PathVariable long id, Model model, @AuthenticationPrincipal PersonDetails pd) {
Comment comment = commentService.findById(id);
if (pd == null || comment.getAuthor().getId() != pd.getPerson().getId())
throw new MismatchIdentifierException("ID автора комментария и Ваш не совпадают. Похоже, что Вы не являетесь автором.");
commentService.deleteById(id);
System.out.println("\n\n\n\ndeleting succesful");
model.addAttribute("task", comment.getTask());
return showTask(comment.getTask().getId(), model, pd);
}
结果,显示一条消息"deleting succesful"
。
服务方式:
public void deleteById(long id) {
commentRepository.deleteById(id);
}
存储库:
@Repository
public interface CommentRepository extends JpaRepository<Comment, Long> {
@Query(value = "SELECT * from comments WHERE task_id = ?1", nativeQuery = true)
List<Comment> findByTask(long id);
@Query(value = "SELECT * from comments WHERE author_id = ?1", nativeQuery = true)
List<Comment> findByAuthor(long id);
Page<Comment> findByAuthor_id(long id, Pageable pageable);
}
我使用Intellij Idea
mvn clean package 收集完全缺少 pom 文件中指定的库(依赖项)的 jar。jar 昵称重达半兆字节。
在这种情况下,jar 工件(Build Artifact..)已完全组装并启动。这里罐子的重量是 47 MB。
这不是我第一次收集项目。
我已经为自己毁掉了整个莫斯科。铲掉项目结构。我知道某处出了问题。但我不知道在哪里。
请帮我往哪个方向挖掘,
下面是pom.xml的内容
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.15</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.destiny</groupId>
<artifactId>startup</artifactId>
<version>0.0.1</version>
<name>Startup/name>
<description>your start will be fine</description>
<properties>
<java.version>11</java.version>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<start-class>com.destiny.startup.StartupApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<!-- исключаем стандартное логгирование спринга, из-за конфликта с log4j2 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<!-- сохраняет сессии ремемберми в БД,-->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
</plugin>
</plugins>
</build>
</project>
构建日志:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< com.destiny:startup >-------------------------
[INFO] Building Startup 0.0.1
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] Parameter 'parameters' is unknown for plugin 'maven-compiler-plugin:3.3:compile (default-compile)'
[WARNING] Parameter 'parameters' is unknown for plugin 'maven-compiler-plugin:3.3:testCompile (default-testCompile)'
[INFO]
[INFO] --- resources:3.2.0:resources (default-resources) @ startup ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 2 resources
[INFO] Copying 61 resources
[INFO]
[INFO] --- compiler:3.3:compile (default-compile) @ startup ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 60 source files to D:\IdeaProjects\SpringStartup\target\classes
[INFO] /D:/IdeaProjects/SpringStartup/src/main/java/com/destiny/startup/config/SecurityConfig.java: D:\IdeaProjects\SpringStartup\src\main\java\com\destiny\startup\config\SecurityConfig.java uses or overrides a deprecated API.
[INFO] /D:/IdeaProjects/SpringStartup/src/main/java/com/destiny/startup/config/SecurityConfig.java: Recompile with -Xlint:deprecation for details.
[INFO]
[INFO] --- resources:3.2.0:testResources (default-testResources) @ startup---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory D:\IdeaProjects\SpringStartup\src\test\resources
[INFO]
[INFO] --- compiler:3.3:testCompile (default-testCompile) @ startup ---
[INFO] No sources to compile
[INFO]
[INFO] --- surefire:2.22.2:test (default-test) @ startup ---
[INFO] No tests to run.
[INFO]
[INFO] --- jar:3.2.2:jar (default-jar) @ startup ---
[INFO] Building jar: D:\IdeaProjects\SpringStartup\target\startup-0.0.1.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.856 s
[INFO] Finished at: 2023-10-20T19:34:03+03:00
[INFO] ------------------------------------------------------------------------
[WARNING]
[WARNING] Plugin validation issues were detected in 4 plugin(s)
[WARNING]
[WARNING] * org.apache.maven.plugins:maven-resources-plugin:3.2.0
[WARNING] * org.apache.maven.plugins:maven-jar-plugin:3.2.2
[WARNING] * org.apache.maven.plugins:maven-compiler-plugin:3.3
[WARNING] * org.apache.maven.plugins:maven-surefire-plugin:2.22.2
[WARNING]
[WARNING] For more or less details, use 'maven.plugin.validation' property with one of the values (case insensitive): [BRIEF, DEFAULT, VERBOSE]
[WARNING]
Process finished with exit code 0
已通过 Spring-Sequrity 进行授权。@Transient
在用户实体中,我对密码验证字段使用注释(代码如下)。该字段最终不会出现在数据库中,但是如果您从存储库中提取该对象,则该字段会出现在那里,这尤其会阻止通过 userRepo.save(userEntity) 进一步保存它:会发生异常:org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
我尝试研究这个问题,是的,事实证明这是正常行为@Transient
。建议显式指示注释@Transient
来源@javax.persistence.Transient
并@org.springframework.data.annotation.Transient
没有帮助,就像声明字段时的瞬态关键字一样。
关于如何防止字段进入private String password2;
实体存储库的任何想法,以便您可以轻松地使用(例如)保存从存储库中获取的该实体?
public String setOnlineData(String username) {
UserEntity userEntity = userRepo.findByUsername(username);
userEntity.setOnlineDate(LocalDateTime.now());
userRepo.save(userEntity); ///exception
return "Successfully";
}
@Entity
@Table(name = "t_user")
@Getter
@Setter
public class UserEntity implements UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@NotBlank(message = "Имя пользователя не может быть пустым!")
private String username;
@NotBlank(message = "Пароль не может быть пустым!")
private String password;
@Transient
@NotBlank(message = "Пароль не может быть пустым!")
private String password2;
private boolean active;
private LocalDateTime regDate;
private LocalDateTime syncDate;
private LocalDateTime onlineDate;
@ManyToOne(fetch = FetchType.LAZY)
private AmalgamationEntity curAmalgamation;
@ElementCollection(targetClass = Role.class, fetch = FetchType.EAGER)
@CollectionTable(name = "t_user_role", joinColumns = @JoinColumn(name = "user_id"))
@Enumerated(EnumType.STRING)
private Set<Role> roles;