1

I have entity class

@Data
@NoArgsConstructor
@Entity
@Table(name = "tx")
public class Tx{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false)
    private int id;
    @Column(name = "aaa")
    private String aaa;
}

and i try to insert 100 template records into db.

Tx tx = applicationContext.getBean(Tx.class);
Session session = sessionFactory.openSession();
session.beginTransaction();
IntStream.range(0, 100).forEach(index -> session.save(tx));
session.getTransaction().commit();
session.close();

but as a result, only 1 record is inserted into the database. What am I doing wrong?

2
  • 3
    Note that saving the same object (with the same id) multiple times, has good chances to end as an update instead . Commented Sep 15, 2021 at 8:56
  • Thank u. I thought that hibernate should create a unique id for each object. I added my own id generation and now it works Commented Sep 15, 2021 at 15:13

2 Answers 2

2

Tx tx = applicationContext.getBean(Tx.class); makes no sense. Beans managed by the Application Context are Controller, Services, Repositories... Things that do something. An Entity represents Data. I'm wondering why this even working try something linke this:

Session session = sessionFactory.openSession();
session.beginTransaction();
IntStream.range(0, 100).forEach(index -> {
   Tx tx = new Tx();
   tx.setAaa(index);
   session.save(tx);
});
session.getTransaction().commit();
session.close();
Sign up to request clarification or add additional context in comments.

1 Comment

thank u for comment, I undestand u, but I'm working with what has already been implemented. I added id generating while bean (scope=prototype) creating and creating new object in each cycle iteration, its work
1

Use CrudRepository<Model, Id> and Call its saveAll(List) Method for this

UserRepository

public interface UserRepository extends CrudRepository<User, Integer> {

    public List<User> findByName(String name);

    public List<User> findByCity(String city);

    public List<User> findByNameAndCity(String name, String city);  

}

Main Method

public static void main(String[] args) {
    ApplicationContext context = SpringApplication.run(BootJpaExampleApplication.class, args);
    UserRepository userRepo = context.getBean(UserRepository.class);
    
    //save Single user 
    userRepo.save(new User(101, "Aniruddh", "Delhi", "I am Java Developer"));
    
    
    // save Multiple Users
    
    User user = new User();
    user.setName("Rahul");
    user.setCity("Bhopal");
    user.setStatus("Python Developer");
    
    User user1 = new User();
    user1.setName("Ram");
    user1.setCity("Jabalpur");
    user1.setStatus("Database Developer");
    
    Iterable<User> users = List.of(user, user1);
    userRepo.saveAll(users);

}

1 Comment

thank u for comment, but I'm working with what has already been implemented, and I'm not ready to rewrite all work with the database under the repositories

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.