简体   繁体   English

Spring Boot 不会增加 long 类型的自动生成的 id

[英]Spring Boot doesn't increment auto-generated id with type long

I'm creating a collection with a Long id type called idPallet which should be incremented.我正在创建一个名为 idPallet 的 Long id 类型的集合,它应该递增。

Pallet.java (collection) Pallet.java(集合)


package com.example.ShippingTest.collections;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.SequenceGenerator;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor

public class Pallet {
    @Id
    @GeneratedValue(generator = "pallet_seq",strategy = GenerationType.IDENTITY)
    @SequenceGenerator(name = "pallet_seq",sequenceName = "pallet",initialValue = 1,allocationSize = 1)
   //@JsonIgnore
    private Long idPallet;
    private int maxPallet;
    private String typePallet;
    private String shipAddress;
    private String shipLocation;
    private String shipCountry;
    private String destination;
    private List<PS> PackingSlip;
}

PalletController托盘控制器

@PostMapping("/pallets")
    public void savePallet(@RequestBody Pallet pallet) {
        palletRepo.save(pallet);
    }

Repository资料库

package com.example.ShippingTest.repositories;

import com.example.ShippingTest.collections.Pallet;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface PalletRepo extends MongoRepository<Pallet,Long> {
    public Pallet findOrderByidPallet(Long idPallet);
}

whenever I try to post a new Pallet, this Exception get displayed: "message": "Cannot autogenerate id of type java.lang.Long for entity of type com.example.ShippingTest.collections.Pallet",每当我尝试发布新托盘时,都会显示此异常:“消息”:“无法为 com.example.ShippingTest.collections.Pallet 类型的实体自动生成 java.lang.Long 类型的 ID”,

I tried this without @SequenceGenerator, and even after using it, the post didn't work.我在没有 @SequenceGenerator 的情况下尝试了这个,即使在使用它之后,帖子也没有用。 when I add @Entity in Pallet.java I get error with obligation of adding another id: 'Basic' attribute type should not be a container.当我在 Pallet.java 中添加 @Entity 时,我收到错误消息,要求添加另一个 id:'Basic' 属性类型不应该是容器。

I think you are missing @Entity annotation.我认为您缺少@Entity注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM