-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Store the same entity in mulitple Lists
PostPosted: Sun Mar 06, 2011 9:10 am 
Newbie

Joined: Sun Mar 06, 2011 8:57 am
Posts: 2
Hi

Im very new to Hibernate so this will probably a easy task for you guys.

As the Topic says I'm trying to reference the same entity in multiple Lists. But when I try to do so I get an exception saying: "Duplicate entry '5' for key 'military_id'".
I googled but could not find a solution to my problem.

I have an Entity called MilitaryUnitData like this:
Code:
@Entity
public class MilitaryUnitData implements IMovable{
   private long Id;
   
//snip
       
   @Id
   @GeneratedValue(strategy=GenerationType.TABLE)
   public long getId() {
      return Id;
   }
   public void setId(long id) {
      Id = id;
   }

       //snip
}


and a class City where I want to store my units in.
Code:
@Entity
public class CityData {

private Collection<MilitaryUnitData> military = new ArrayList<MilitaryUnitData>();
private String name;

//snip
       
@Id
   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

@OneToMany
   @Column(nullable=false)
   public Collection<MilitaryUnitData> getMilitary() {
      return military;
   }

   public void setMilitary(Collection<MilitaryUnitData> military) {
      this.military = military;
   }

//snip
}


The problem occurs when I want to put a Unit into 2 cities at the same time.
How do I have to change the mapping to achive this?

Thx in advance.


Top
 Profile  
 
 Post subject: Re: Store the same entity in mulitple Lists
PostPosted: Sun Mar 06, 2011 7:14 pm 
Regular
Regular

Joined: Sun Aug 01, 2004 6:49 pm
Posts: 76
How do you fetch, modify and persist your cities and groups? What does your mapping look like?


Top
 Profile  
 
 Post subject: Re: Store the same entity in mulitple Lists
PostPosted: Mon Mar 07, 2011 6:15 am 
Newbie

Joined: Sun Mar 06, 2011 8:57 am
Posts: 2
My mapping can be seen in the code snippits. Im using JPA annotations. Or what else do you need to know?.

An Example for fetch, modify, persist would be:
Code:
@Test
   public void fuckTest()
   {
      EntityManager em = Database.getEntityManager();
      EntityTransaction tx = null;
      
      try
      {
         tx = em.getTransaction();
         tx.begin();
         
         MilitaryUnitData unit1 = new MilitaryUnitData();
         em.persist(unit1);
         
         //get all the units involved in the battle
         Query siegUnitsQuery = em.createQuery("FROM CityData as cityData");
         
         ArrayList<CityData> unitList = (ArrayList<CityData>)siegUnitsQuery.getResultList();
         
         for(CityData city : unitList)
            city.getMilitary().add(unit1);
      
         tx.commit();
      }
      catch (RuntimeException e) {
          if ( tx != null && tx.isActive() ) tx.rollback();
         
          System.out.print("anal");
      }
      finally {
          em.close();
      }   
   }


I however already tried different things where I only add a unit to one city at a time (meaning in a single commit). But that did not change anything.


Top
 Profile  
 
 Post subject: Re: Store the same entity in mulitple Lists
PostPosted: Wed Mar 09, 2011 1:37 pm 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Hi,

If you want a unit to be in 2 cities at the same time shouldn't you use a many-to-many to map your relations instead of a one-to-many?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.