-->
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.  [ 2 posts ] 
Author Message
 Post subject: Edit primary key, if it's a foreign key
PostPosted: Mon Apr 12, 2010 4:22 am 
Newbie

Joined: Fri Dec 11, 2009 1:23 pm
Posts: 5
I have a problem: I can't know, how edit primary key, if this key is a foreign key...
ER:
Image
I want to edit curseId, but I can't...

User.java
Code:
@Entity
@Table(name = "USERS")
public class User implements Serializable
{
  @Id
  @Column(name = "USER_ID", nullable = false, length = 10)
  private String userId;
  @Column(name = "NAME_AT_BIRTH", nullable = false)
  private String nameAtBirth;
  @Column(name = "LAST_NAME", nullable = false)
  private String lastName;
  @Column(name = "FIRST_NAME", nullable = false)
  private String firstName;
  @Column(name = "MOTHERS_NAME", nullable = false)
  private String mothersName;
  @Column(name = "PERM_ADDRESS", nullable = false)
  private String permAddress;
  @Column(name = "TEMP_ADDRESS")
  private String tempAddress;
  @Temporal(value = TemporalType.DATE)
  @Column(name = "BIRTHDATE", nullable = false, length = 7)
  private Date birthdate;
  @Column(name = "PASSWORD", nullable = false, length = 32)
  private String password;
  @Column(name = "EMAIL", length = 320)
  private String email;
  @Temporal(value = TemporalType.DATE)
  @Column(name = "START_DATE", nullable = false, length = 7)
  private Date startDate;
  @Temporal(value = TemporalType.DATE)
  @Column(name = "END_DATE", length = 7)
  private Date endDate;
  @Column(name = "TYPE", length = 16)
  private String type;
  @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user")
  private List<UserCheck> checks = new ArrayList<UserCheck>();
  @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user")
  private List<UserCurse> curses = new ArrayList<UserCurse>();
}


Curse.java
Code:
@Entity
@Table(name = "CURSES")
public class Curse implements Serializable
{
  @EmbeddedId
  @AttributeOverrides(
  {
    @AttributeOverride(name = "curseId", column = @Column(name = "CURSE_ID", nullable = false, length = 16)),
    @AttributeOverride(name = "semesterId", column = @Column(name = "SEMESTER_ID", nullable = false, length = 10))
  })
  private CurseId id;
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "SEMESTER_ID", nullable = false, insertable = false, updatable = false)
  private Semester semester;
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "ROOM_ID", nullable = true)
  private Room room;
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "SUBJECT_ID", nullable = false)
  private Subject subject;
  @Column(name = "TIME", nullable = true, length = 32)
  private String time;
  @Column(name = "STUDENTS_LIMIT", precision = 10, scale = 0, nullable = true)
  private Long studentsLimit;
  @OneToOne
  @JoinColumns(value =
  {
    @JoinColumn(name = "CURSE_ID", referencedColumnName = "CURSE_ID", nullable = false, insertable = false, updatable = false),
    @JoinColumn(name = "SEMESTER_ID", referencedColumnName = "SEMESTER_ID", nullable = false, insertable = false, updatable = false)
  })
  private CurseUserCountView curseUserCountView;
  @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "curse")
  private List<UserCurse> users = new ArrayList<UserCurse>();
  @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "curse")
  private List<Check> checks = new ArrayList<Check>();
}


CurseId.java
Code:
@Embeddable
public class CurseId implements Serializable
{
  @Column(name = "CURSE_ID", nullable = false, length = 16)
  private String curseId;
  @Column(name = "SEMESTER_ID", nullable = false, length = 10)
  private String semesterId;
}


UserCurse.java
Code:
@Entity
@Table(name = "USERS_CURSES")
public class UserCurse implements Serializable
{
  @EmbeddedId
  @AttributeOverrides(
  {
    @AttributeOverride(name = "userId", column = @Column(name = "USER_ID", nullable = false, length = 16)),
    @AttributeOverride(name = "curseId", column = @Column(name = "CURSE_ID", nullable = false, length = 16)),
    @AttributeOverride(name = "semesterId", column = @Column(name = "SEMESTER_ID", nullable = false, length = 10))
  })
  private UserCurseId id;
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "USER_ID", nullable = false, insertable = false, updatable = false)
  private User user;
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumns(value =
  {
    @JoinColumn(name = "CURSE_ID", referencedColumnName = "CURSE_ID", nullable = false, insertable = false, updatable = false),
    @JoinColumn(name = "SEMESTER_ID", referencedColumnName = "SEMESTER_ID", nullable = false, insertable = false, updatable = false)
  })
  private Curse curse;
  @Column(name = "PERFORMED", precision = 1, scale = 0)
  private Boolean performed;
}


UserCurseId.java
Code:
@Embeddable
public class UserCurseId implements Serializable
{
  @Column(name = "USER_ID", nullable = false, length = 16)
  private String userId;
  @Column(name = "CURSE_ID", nullable = false, length = 16)
  private String curseId;
  @Column(name = "SEMESTER_ID", nullable = false, length = 10)
  private String semesterId;
}


Thanks for helping!


Top
 Profile  
 
 Post subject: Re: Edit primary key, if it's a foreign key
PostPosted: Fri May 07, 2010 3:43 am 
Newbie

Joined: Wed Apr 21, 2010 8:41 am
Posts: 12
Hallo,

ich antworte mal auf deutsch....

also so ganz versteh ich das Problem noch nicht aber ich denke es macht keinen Sinn eine ID zu editieren. Dabei fällt mir auch direkt auf das du garkeine ID-Generatoren in deinen Entities hast. Zum Beispiel in deiner User Entity unter @Id -> @GeneratedValue(strategy=GenerationType.AUTO)

oder für Foreign Keys in Entity Curse sowas:

@GeneratedValue(generator = "myForeignGenerator")
@GenericGenerator(name = "myForeignGenerator", strategy = "foreign", parameters = @org.hibernate.annotations.Parameter(name = "property", value = "userCurse"))

Sobald jede Entity einen ID-Generator hat brauchst du keine IDs selber setzen. Evtl. musst du für deine EmbeddedIds einen eigenen ID-Generator schreiben.

Grüße,
Jonas


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.