gpt4 book ai didi

java - 为什么在给mvn clean程序包时出现此错误:找不到表?

转载 作者:太空宇宙 更新时间:2023-11-04 09:41:10 25 4
gpt4 key购买 nike

我正在建立用户之间的友谊,一个用户可以有任何朋友,有朋友列表,
我正在使用摇摇欲坠来发出请求,但出现错误500,值为空,我不知道为什么

这个错误的控制器,我使用请求后

@CrossOrigin(origins = "http://localhost:4200", maxAge = 3600)
@RestController
@RequestMapping({"/user"})
public class UserController {

@Autowired
UserServiceImpl userServiceImpl;
static Logger log = Logger.getLogger(UserController.class.getName());

@PostMapping("/create")
public ResponseEntity<User> createUser(@RequestBody User user) {
log.info("CREATE");
return new ResponseEntity<User>(userServiceImpl.createUser(user), HttpStatus.OK);
}


@PostMapping("/addFriend")
public ResponseEntity<User> addFriend(@RequestBody Friendship friendship) {
log.info("addFriend");
return new ResponseEntity<User>(userServiceImpl.createLinkWithFriends(friendship), HttpStatus.OK);
}

@GetMapping("/getUsers")
public ResponseEntity<List<User>> getUsers() {
log.info("getUser");
return new ResponseEntity<>(userServiceImpl.getUsers(), HttpStatus.OK);
}

@GetMapping("/showFriends/{id}")
public ResponseEntity<List<User>> getUsers(@PathVariable("id") int id) {
log.info("showFriends"+id);
return new ResponseEntity<>(userServiceImpl.getUsers(), HttpStatus.OK);
}

}


这是我的服务:

@Service
public class UserServiceImpl {
@Autowired
UserRepository userRepository;
@Autowired
public FriendsRepository friendsRepository;

public User createUser(User user) {

if (userRepository.findByEmail(user.getEmail()) == null) {
return userRepository.save(user);
}
return null;
}

public User getUser(int index) {
return null;
}

public List<User> getUsers() {
return userRepository.findAll();
}



public User createLinkWithFriends(Friendship friendship) {
User owner=userRepository.findById(friendship.getOwner().getId());
User friend=userRepository.findById(friendship.getFriend().getId());
owner.addFriend(friend);

friendsRepository.save(friendship);
return owner;
}
}


这是存储库用户

@Entity
@Table(name = "User")
public class User implements Serializable {

private static final long serialVersionUID = -3009157732242241606L;
@Id
//@GeneratedValue(strategy = GenerationType.IDENTITY)
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

@Column(name = "name")
private String name;

@Column(name = "password")
private String password;

@Column(name = "email")
private String email;

@OneToMany(fetch=FetchType.EAGER)
@JoinTable(
name = "Friendship",
joinColumns = @JoinColumn(name = "id"),
inverseJoinColumns = @JoinColumn(name = "id_friend"))
private Set<User> friends = new HashSet<>();

public User() {

}

public User(String name, long id, String password, String email) {
this.email = email;
this.name = name;
this.id = id;
this.password = password;
}

public void addFriend(User userFriend){
this.friends.add(userFriend);
}

public Set<User> getFriends() {
return friends;
}

public void setFriends(Set<User> friends) {
this.friends = friends;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public User(String name) {
this.name = name;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}


这就是友谊

@Entity
@Table(name = "Friendship")
public class Friendship implements Serializable {

private static final long serialVersionUID = -3009157732242241606L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
//@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id_friend;

@ManyToOne
private User owner;

@ManyToOne
private User friend;

public Friendship() {

}

public long getId_friend() {
return id_friend;
}

public void setId_friend(long id_friend) {
this.id_friend = id_friend;
}

public User getOwner() {
return owner;
}

public void setOwner(User owner) {
this.owner = owner;
}

public User getFriend() {
return friend;
}

public void setFriend(User friend) {
this.friend = friend;
}
}


我收到此错误,为什么我找不到表友谊?这个错误是当我使 mvn clean package

   2019-05-03 23:54:59.925  INFO 20200 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.0.12.Final}
2019-05-03 23:54:59.925 INFO 20200 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2019-05-03 23:54:59.931 INFO 20200 --- [ main] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2019-05-03 23:54:59.979 INFO 20200 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2019-05-03 23:55:00.132 INFO 20200 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2019-05-03 23:55:01.094 INFO 20200 --- [ main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000228: Running hbm2ddl schema update
2019-05-03 23:55:01.132 INFO 20200 --- [ main] rmationExtractorJdbcDatabaseMetaDataImpl : HHH000262: Table not found: friendship
2019-05-03 23:55:01.133 INFO 20200 --- [ main] rmationExtractorJdbcDatabaseMetaDataImpl : HHH000262: Table not found: friendship
2019-05-03 23:55:01.136 WARN 20200 --- [ main] o.s.w.c.s.GenericWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.sp
ringframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/Hibern
ateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFac
tory
2019-05-03 23:55:01.152 INFO 20200 --- [ main] utoConfigurationReportLoggingInitializer :

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2019-05-03 23:55:01.184 ERROR 20200 --- [ main] o.s.boot.SpringApplication : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/
HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate Sess
ionFactory


文件application.properties具有以下内容:

spring.h2.console.enabled=true
spring.h2.console.path=/h2_console
spring.datasource.url=jdbc:h2:file:~/test
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create

最佳答案

在我看来,插入后为null的连接表(friendship.id_friend)的ID似乎正确。数据库应该在插入过程中将此空值转换为实际的ID,但这不会发生并引起异常。

仔细检查friendship表的设置方式,特别是id_friend列是“自动增量”还是具有类似的触发器。

关于java - 为什么在给mvn clean程序包时出现此错误:找不到表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55977830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com