0

My problem that when I make cascade persist it throws exception (MySQLIntegrityConstraintViolationException). I am using Spring Data; for persisting my data.

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'Bloomberg' for key 'PRIMARY'

News.class:

@Entity
public class News implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "company_name")
    private Company company;

    ...
}

Company.class:

@Entity
@Table(name = "company")
public class Company implements Serializable{

    @Id
    @Column(unique = true,nullable = false)
    private String name;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "company",fetch = FetchType.EAGER)
    private List<News> newsList;

   ...
}

I am using this code to persist :

public void parseDB() throws IOException {
    List<News> newsList = new ArrayList<>();

    Map<String, Company> companyMap = ((List<Company>) companyRepository.findAll()).stream().collect(Collectors.toMap(Company::getName, company -> company));

    newsList.addAll(WebPagesParser.parseCategory("...", "...", companyMap));
    newsList.forEach(newsService::create);
    }


public static List<News> parseCategory(String url, String category,Map<String,Company> companyMap) throws IOException {
       ... 
      Company company; 
      if (companyMap.containsKey(name)) {
                    company = companyMap.get(name);
                } else {
                    company = new Company();
                    company.setName(top.get(1).text());
                }
       news.setCompany(company);
      ...
      }

And it gave me new exception

org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.example.inosmi.database.data.News; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.example.inosmi.database.data.News

Done: Have solved half of my problem, I still don't know where it is but it works. Thanks everyone!

2 Answers2

0

MySQL is telling you that you're trying to insert multiple rows with the same primary key

The code that this relates to is here

@Id
@Column(unique = true, nullable = false)
private String name;

And the data you're loading has multiple rows with the company name Bloomberg

ptomli
  • 11,730
  • 4
  • 40
  • 68
  • I am completely understanding it, and I want it to merge this companies objects in one entity. – Давид Бестаев Oct 16 '17 at 13:56
  • Then make sure you're loading the company object, not creating a new one. When you call `News#setCompany` the argument must be one that you've already loaded from the database, not one you just created with `new Company(...)` – ptomli Oct 16 '17 at 13:57
  • And top result for Googling that error points to https://stackoverflow.com/q/13370221/134894 – ptomli Oct 16 '17 at 14:30
0

Use Set instead of List for @OneToMany else maintain one index column

@OneToMany(cascade = CascadeType.ALL, mappedBy = "company",fetch = FetchType.EAGER)
private Set<News> newsList;