Giter VIP home page Giter VIP logo

jpabulkinsert's Introduction

Bulk Insert Entity

The BulkInsertEntity abstract class is designed to facilitate bulk insert operations in JPA entities.

It provides common fields and functionality required for bulk inserts, allowing entities to be saved in batches using saveAll method provided by JpaRepository.

Usage

To use the BulkInsertEntity abstract class:

  1. Create your entity class by extending BulkInsertEntity.
  2. Use JpaRepository provided by Spring Data JPA to save entities in bulk using the saveAll method.

BulkInsertEntity

@Getter
@ToString
@MappedSuperclass
@Audited
@EntityListeners(AuditingEntityListener.class)
public abstract class BulkInsertEntity implements Persistable<Long> {

    @Transient
    private boolean isNew = true;

    @Override
    public boolean isNew() {
        return isNew;
    }

    @PostLoad
    private void markIsNotNew() {
        isNew = false;
    }

    @CreatedDate
    @Column(nullable = false, updatable = false)
    protected LocalDateTime createdDatetime;

    @LastModifiedDate
    @Column(nullable = false)
    protected LocalDateTime updatedDatetime;

    @CreatedBy
    protected Long createdBy;

    @LastModifiedBy
    protected Long updatedBy;

}

Example

@Entity
@Getter
@ToString
@Builder
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "Books")
public class Book extends BulkInsertEntity {

    @Id
    @Column
    private Long bookSeq;

    @Column
    private String title;

    @Column
    private String author;

    @Column
    private LocalDateTime publicationDate;

    @Column(precision = 20, scale = 5)
    private BigDecimal price;

    @Override
    public Long getId() {
        return bookSeq;
    }

}
public interface BookRepository extends JpaRepository<Book, Long> {
    // No additional methods required
}
@Service
public class BookService {

    private final BookRepository repository;

    @Autowired
    public BookService(BookRepository repository) {
        this.repository = repository;
    }

    public void saveEntitiesInBulk(List<Book> entities) {
        repository.saveAll(entities);
    }
}

Dependencies

  • Spring Data JPA: For data access and repository support
  • Lombok: For generating boilerplate code

License

This project is licensed under the MIT License - see the LICENSE file for details.

jpabulkinsert's People

Contributors

baker-1ee avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.