UcanDoIt
UcanDoIt

Reputation: 1845

Trying to persist an object in mysql database

I have created a mySQL database with some tables, the relevant ones for my problem are Players and Season.

Table season contain:

Season_id (int),seasonNumber(int),total(int), week(int), value(int), totalsell(int)

I have my Java code, where i did create my entities and using anotations i identified the entities as well as my primary key for each entitie.

Here are the atributes of my Season POJO class:

private int id;
private LinkedList<Player> keepers;
private LinkedList<Player> defs;
private LinkedList<Player> inners;
private LinkedList<Player> wings;
private LinkedList<Player> innersTreino;
private LinkedList<Player> strikers;
private LinkedList<Player> strikersTreino;
private int seasonNumber;
private LinkedList<Player> vendas;
private LinkedList<Player> sold;
private int total;
private int week;
private int value; 
private int totalsell;

After that i created my entitymanagerFactory and my entity manager, and try to test if i can fill my database:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("htTeamPU");
  EntityManager em = emf.createEntityManager();

  em.getTransaction().begin();
  Season test = new Season();
  test.setSeasonNumber(43);
  test.setTotal(5);
  test.setWeek(100);
  test.setValue(200);
  test.setTotalsell(10);

  em.persist(test);
  em.getTransaction().commit();
  em.close();
  emf.close();

When i run my program i have an exception:

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'DEFS' in 'field list'

The problem seems to be that my object as 9 linkedLists of Players, and i don't have them represented on my season table. I don't really know how to represent the linked lists on my season table to solve my problem.

Upvotes: 0

Views: 229

Answers (1)

Jman
Jman

Reputation: 496

You should make many-to-many relationship with Player and Seoson tables.

For one of your lists;

At the owning side (Seoson)

@ManyToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(name = "seoson_keepers", 
            joinColumns = { @JoinColumn(name = "Seoson_id")}, 
            inverseJoinColumns={@JoinColumn(name="Player_id")})  
private List<Player> keepers;

At the inverse side (Player)

@ManyToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE}, 
            mappedBy = "keepers")
private List<Seoson> seosons; 

You can find more details about many-to-many relationships in jpa here.

According to your design, you must repeat above code for all your lists.

Upvotes: 2

Related Questions