-->
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: Problem with Key
PostPosted: Wed Aug 15, 2007 2:47 pm 
Newbie

Joined: Tue Aug 14, 2007 11:47 am
Posts: 6
Hi,

I'm new in this forum and start using Hibernate recently. I will try to detail my problem the best I can.

First: I have two persistent classes: Page and Category.
Second: Into Page class contains one instance of Category class. See example below.

Code:
   
public class Page {
     private int code;
     private String name;
     private Category category;

    // Getters and setters methods
    ....
  }

  public class Category {
    private int code;
    private String name;
  }


Third: In my database the tables are mapped. Its are working correctly.

This is my context. My problem is when the Page is stored. The table mapped by the Category class are populated with data.

When I try to insert a Page, a key violation happens in Category Class. How I can store a Page using a Category which already exists in database?. Example

Code:
Category cat = new Category();
cat.setCode(500) // Code 500 already exist in database

Page page = new Page();
page.setName("Test Page");
page.setCategory(cat);

HibernateUtil.store(page) //Violation of key here


How can I do it ??
tanks

_________________
Surfing the life!!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 16, 2007 4:30 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You need to post your mapping files for these classes.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 11:22 am 
Newbie

Joined: Tue Aug 14, 2007 11:47 am
Posts: 6
Okay,
This is the code of my PageDTO class.
Code:
@Entity
@Table(name = "page")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class PageDTO extends DTO {

  private CategoryDTO           category;
  private Date                        dateCreation;
  private Integer                    height;
  private String                      name;


  // Constructor
  public PageDTO() {
  }

  // Methods
  @OneToMany(mappedBy = "parent")
  public List<PageDTO> getChildren() {
    return this.children;
  }

  @OneToOne(cascade = CascadeType.ALL)
  @JoinColumn(name = "category_id")
  public CategoryDTO getCategory() {
    return this.category;
  }

  @Temporal(TemporalType.DATE)
  @Column(name = "pag_date_creation", nullable = false)
  public Date getDateCreation() {
    return this.dateCreation;
  }

  @Basic
  @Column(name = "pag_height", nullable = false)
  public Integer getHeight() {
    return this.height;
  }

  @Basic
  @Column(name = "pag_name", nullable = false, length = 50)
  public String getName() {
    return this.name;
  }

  public void setCategory(CategoryDTO category) {
    this.category = category;
  }

  public void setDateCreation(Date dateCreation) {
    this.dateCreation = dateCreation;
  }

  public void setHeight(Integer height) {
    this.height = height;
  }

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

}



Now. This is the code of CategoryDTO class
Code:
@Entity
@Table(name="category")
public class CategoryDTO extends DTO {

  // Atributos da Classe
  private static final long serialVersionUID = -776137723692955756L;
  private String            description;
 
  // Construtors
  public CategoryDTO() {
   
  }
 
  public CategoryDTO(String description) {
    this.setDescription(description);
  }
 
  //Métodos
  @Basic
  @Column(name="cat_description", nullable = false, length = 50)
  public String getDescription() {
    return this.description;
  }

  public void setDescription(String description) {
    this.description = description;
  }
}



DTO class contains the attribute named code;

I want insert two pages in same category!!
page1 -> category1
page2 -> category1 // Key violation here!!

example:
Code:
CategoryDTO cat = new CategoryDTO("test")
cat.setCode(1);

PageDTO p1 = new PageDTO();
pageDTO p2 = new PageDTO();

p1.setCategory(cat);
p2.setCategory(cat);

HibernateUtil.store(p1); // Work
HibernateUtil.store(p2); // Key violation in category



Tanks!!

_________________
Surfing the life!!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 18, 2007 4:28 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You've mapped category as OneToOne in Page but you're trying to link 2 pages to one category. Page->Category should therefor be ManyToOne.


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.