Home > Enterprise >  Best practice for insert data into relation table with many columns (no ORM)
Best practice for insert data into relation table with many columns (no ORM)

Time:10-27

I have table REPORT. This table have relation with (for example) table CLIENT and table TAG.

In the request I get one REPORT with 1-20 CLIENT and 1-20 TAG. I need insert it in DB (postgre).

How I can do it if I can use only JdbcTemplate? (All ORM forbidden). Of course with transactional and rollback? I need only some idea.

CodePudding user response:

Report Model class

@Entity
@Table(name = "reports")
public class Report {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private String name;

    @OneToMany(mappedBy="report")
    private List<Client> clients;

    @OneToMany(mappedBy="report")
    private List<Tag> tags;
}

Client Model class

@Entity
@Table(name = "clients")
public class Client {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
    @JoinColumn(name = "report_id")
    private Report report;
}

Tag Model class

@Entity
@Table(name = "tags")
public class Tag {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String tagName;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
    @JoinColumn(name = "report_id")
    private Report report;
}

Report Controller

Report report = new Report();
        report.setName("Report");

        List<Client> clientList = new ArrayList<>();
        Client client1 = new Client();
        client1.setName("user1");
        Client client2 = new Client();
        client2.setName("user2");
        clientList.add(client1);
        clientList.add(client2);

        List<Tag> tagList = new ArrayList<>();
        Tag tag1 = new Tag();
        tag1.setTagName("tag1");
        tagList.add(tag1);

        report.setClients(clientList);
        report.setTags(tagList);

        Report resultReport = reportRepository.save(report);

Use this as reference, it will work.

CodePudding user response:

Avoiding ORM, a way forward would be using JDBCTemplate, which I am assuming will be injected into the 3 @Repositories below:

@Repository
public class TagRepository {

    public void insertreport(Report report) {
        .. do an insert using @JdbcTemplate
    }

}

@Repository
public class TagRepository {

    public void insertTags(List<Tag> tags) {
        .. do a bulk insert using @JdbcTemplate
    }   

}

@Repository
public class ClientRepository {

    public void insertClients(List<Client> clients) {
        .. do a bulk insert using @JdbcTemplate
    }

}

@Service
public class ReportService {

    @Autowire 3 @Repository above

    @Transactional
    public void addReport(Report report) {
        reportRepository.insertReport(report);
        tagRepository.insertTags(report.getTags());
        clientRepository.insertClients(report.getClients());
    }

}

Quite verbose code, but split into @Repositories reasonable encapsulation can make it tolerable.

Please note: JdbcTemplate is best used towards a 'stable' database where the data structure will not change.

  • Related