我有两个不同的Spring Boot Rest应用程序,第一个应用程序用于Order,第二个应用程序用于Users。我在两个应用程序中都使用DBSeeder来填充一些初始值。订单应用程序DBSeeder可以正常工作,我可以保存订单列表,但是用户应用程序DbSeeder不接受用户列表。 我只想知道为什么Dbseeder Order接受订单列表,为什么User Dbseeder不接受用户列表,这两者之间有什么区别:-
订单申请:- DbSeeder.java:-
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import model.Item;
import model.Order;
@Component
public class DbSeeder implements CommandLineRunner {
private AccountRepository accountRepository;
private OrderRepository orderRepository;
private InventoryRepository inventoryRepository;
public DbSeeder(OrderRepository orderRepository) {
super();
this.orderRepository = orderRepository;
}
@Override
public void run(String... strings) throws Exception {
Order or1 = new Order(111, "AX-121UIZO", new Date(2018, 12, 06), Arrays.asList(new Item(123456, 6),new Item(178456, 5)));
Order or2 = new Order(112, "GH-1YUGHYO", new Date(2018, 07, 28), Arrays.asList(new Item(123456, 1),new Item(178456, 3)));
Order or3 = new Order(113, "IJ-YUIUIZO", new Date(2018, 12, 21), Arrays.asList(new Item(123456, 4),new Item(178456, 2)));
this.orderRepository.deleteAll();
List<Order> orders = Arrays.asList(or1,or2,or3);
this.orderRepository.save(orders);
}
}
上面的DbSeeder最后一行代码
this.orderRepository.save(orders);
没有给出任何错误。
但是对于下面的用户应用程序DbSeeder,我在最后一行得到了错误
this.userRepository.save(users);
错误是
此行有多个标记 -CrudRepository类型的save(S)方法不适用于参数(列表)
这是User应用程序的完整DbSeeder代码:-
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import com.ipaylabs.parent.model.User;
import com.ipaylabs.parent.repositories.UserRepository;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@Component
public class DbSeeder implements CommandLineRunner {
private UserRepository userRepository;
public DbSeeder(UserRepository userRepository) {
super();
this.userRepository = userRepository;
}
@Override
public void run(String... strings) throws Exception {
User u1 = new User("arvind", "181994", "arvindcarpenter94@gmail.com", "121");
User u2 = new User("nakul", "181994", "arvindcarpenter94@gmail.com", "121");
User u3 = new User("nidhi", "181994", "arvindcarpenter94@gmail.com", "121");
this.userRepository.deleteAll();
List<User> users = Arrays.asList(u1,u2,u3);
this.userRepository.save(users);
}
}
以下是订单和存储库
@Repository
public interface OrderRepository extends MongoRepository<Order,String>{
}
UserRepository:-
@Repository
public interface UserRepository extends MongoRepository<User, String> {
}
用户和订单的模型类是:-
Order.java
@Document(collection = "Order")
public class Order {
@Id
private String id;
private int customerId;
private String orderId;
private Date orderDate;
private List<Item> items;
protected Order() {}
public Order(int customerId, String orderId, Date orderDate, List<Item> items) {
super();
this.customerId = customerId;
this.orderId = orderId;
this.orderDate = orderDate;
this.items = items;
}
public String getId() {
return id;
}
public int getCustomerId() {
return customerId;
}
public String getOrderId() {
return orderId;
}
public Date getOrderDate() {
return orderDate;
}
public List<Item> getItems() {
return items;
}
public void setId(String id) {
this.id = id;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
public void setItems(List<Item> items) {
this.items = items;
}
}
User.java
@Document(collection="User")
public class User {
@Id
private String id;
@Indexed(direction=IndexDirection.ASCENDING)
private String username;
private String password;
private String salt;
private String email;
private String lastLoginTime;
public User() {}
public User(String username, String password, String email, String lastLoginTime) {
super();
this.username = username;
this.password = password;
this.email = email;
this.lastLoginTime =lastLoginTime;
}
public String getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getEmail() {
return email;
}
public String getLastLoginTime() {
return lastLoginTime;
}
public void setId(String id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setEmail(String email) {
this.email = email;
}
public void setLastLoginTime(String lastLoginTime) {
this.lastLoginTime = lastLoginTime;
}
public String getSalt() {
return salt;
}
public void setSalt(String salt) {
this.salt = salt;
}
}