automatyczne generowanie numerów id

0

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";
    }
}

1

Masz GenerationType.AUTO czyli provider wybiera jak ci będzie generował te ID w zalezności od typu ID + bazy jakiej używasz. Może być tak, że używa sekwencji i po prostu już przebił się przez numer 1, albo po prostu pominął numer 1, tzn. pobrał sobie za wczasu i potem zrestartowałeś czy coś bez zapisania i ID przepadło.

Generalnie bym się tym nie martwił, gapy w ID to nic szczególnego o ile nie są jakieś ogromne. Jak np, w Hibernate zostawisz domyślny allocationSize = 50 i stare generatory ID to potem gapy potrafią być w milionach.

1

a w produkcie 1 a przecież powinienem mieć 1

Brzmi że jest w porządku :)

Zgaduję, że chodziło Tobie że w obu przypadkach powinno być 1. Jest używany współna sekwencja, możesz do każdej tabeli zdefiniować customowy, ale nie ma to znaczenia.

0
Kermii napisał(a):

a w produkcie 1 a przecież powinienem mieć 1

Brzmi że jest w porządku :)

Zgaduję, że chodziło Tobie że w obu przypadkach powinno być 1. Jest używany współna sekwencja, możesz do każdej tabeli zdefiniować customowy, ale nie ma to znaczenia.

Właśnie o to mi chodziło żeby w obu było jeden. Mógłbyś mi przesłać jak można by to zrobić?

1

Tutaj znajdziesz przykład:
https://stackoverflow.com/questions/3068692/hibernate-sequence-on-oracle-generatedvaluestrategy-generationtype-auto

@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="my_entity_seq_gen")
@SequenceGenerator(name="my_entity_seq_gen", sequenceName="MY_ENTITY_SEQ")
private Long id;

Sekwencja powinna być zdefiniowana na bazie

1 użytkowników online, w tym zalogowanych: 0, gości: 1