Mam dość dziwny problem otóż kiedy odpalam projekt i z poziomu projektu dodaje dane do bazy danych to mimo tego że mam w entities
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
to gdy najpierw wejdę na widok /products a potem /users to w tabeli user mam id usera 2 a w produkcie 1 a przecież powinienem mieć 1 bo są automatycznie generowane id
@Entity
public class User implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long userId;
public User() {
}
public User(String firstname, String lastname, String email, Date birthday) {
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
this.birthday = birthday;
}
@OneToMany(mappedBy = "user")
private List<Orders> orders = new ArrayList<Orders>();
public long getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
@NotEmpty
@Size(min=3)
private String firstname;
@NotEmpty
@Size(min=3)
private String lastname;
@NotEmpty @Email
private String email;
@NotNull
private Date birthday;
public String getFirstname() {
return firstname;
}
public String getEmail() {
return email;
}
public Date getBirthday() {
return birthday;
}
public long getUserid() {
return userId;
}
public String getLastname() {
return lastname;
}
public void setUserid(int user_id) {
this.userId = user_id;
}
public void setFirstname(String name) {
this.firstname = name;
}
public void setLastname(String surname) {
this.lastname = surname;
}
}
@Entity
public class Product {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long productId;
public Product( ) {
}
public Product( String name, String description) {
this.name = name;
this.description = description;
}
@OneToMany(mappedBy = "orderdetailsId")
private List<OrderDetails> orderdetails = new ArrayList<OrderDetails>();
private String name;
private String description;
public String getDescription() {
return description;
}
public long getProductid() {
return productId;
}
public String getName() {
return name;
}
public void setProductid(Integer product_id) {
this.productId = product_id;
}
public void setName(String name) {
this.name = name;
}
}
@Controller
public class UserController{
@Autowired // This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to handle the data
private UserRepository userRepository;
@RequestMapping("/users")
String products() throws ParseException{
SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = dateformat3.parse("09/09/1999");
userRepository.save(new User("Xxxx","Xxxxski","[email protected]", date1) );
return "users";
}
@Controller
public class ProductController {
@Autowired
private ProductRepository productRepository;
@RequestMapping("/products")
String products(){
productRepository.save(new Product("Lenovo y580","Laptop"));
return "products";
}
}