-
1. Re: Composite Key / Foreign Key EJB3
lhoriman Jul 18, 2005 3:23 PM (in response to kostaboss)This is what works for me. I have Person, Question, and Response (pk is personid, questionid).
@Embeddable(access=AccessType.FIELD) public class ResponsePK implements java.io.Serializable { /** */ Long personId; Long questionId;
and...@Entity(access=AccessType.FIELD) @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) public class Response implements java.io.Serializable { /** */ @Id(generate=GeneratorType.NONE) ResponsePK pk; @Column(nullable=false) float answer; @Column(nullable=false) Date changed; /** Note this "overlaps" with the PK */ @ManyToOne @JoinColumn(name="personId", insertable=false, updatable=false) Person person; /** Note this "overlaps" with the PK */ @ManyToOne @JoinColumn(name="questionId", insertable=false, updatable=false) Question question;
Should be fairly self-explanatory.
Jeff Schnitzer
http://www.similarity.net -
2. Re: Composite Key / Foreign Key EJB3
kostaboss Jul 18, 2005 5:22 PM (in response to kostaboss)unfortunatly this did not do the trick ...
i added "@Embeddable(access=AccessType.FIELD)" in the Person entity
and adjusted the Personrelationship to look like this:@Entity(access=AccessType.FIELD) @Table(name = "person_Relationship") @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class Personrelationship implements Serializable { // Fields private Person person; private Person relperson; private String personrelshipcd; // Property accessors /** * @return Returns the person. */ @ManyToOne @JoinColumn(name = "person_id", insertable=false, updatable=false) public Person getPerson() { return person; } /** * @param person * The person to set. */ public void setPerson(Person person) { this.person = person; } /** * @return Returns the relperson. */ @ManyToOne @JoinColumn(name = "person_id", insertable=false, updatable=false) public Person getRelperson() { return relperson; } /** * @param relperson * The relperson to set. */ public void setRelperson(Person relperson) { this.relperson = relperson; } /** * @return Returns the personrelshipcd. */ @Column(name = "Person_relship_cd", length = 40, nullable = false) public String getPersonrelshipcd() { return personrelshipcd; } /** * @param personrelshipcd * The personrelshipcd to set. */ public void setPersonrelshipcd(String personrelshipcd) { this.personrelshipcd = personrelshipcd; } }
unfortunatly i still get a
org.hibernate.AnnotationException: No identifier specified for entity: Personrelationship
on deployment.
The point is that I want to make an Entity that obviously holds an Relationship between two Persons (same classtype) in the Database.
this Relationship is uniquely identified and adequately described throu its
person and relperson (primary key/forein key) entry.
So the point is how to make them the composite key of the table..
as i saw in your code example you had an addtional Primkey field@Id(generate=GeneratorType.NONE) ResponsePK pk;
I was trying to avoid that cause its not needed.
So I don't know how to do this with EJB3.0 annotation.
thankfull for any suggestions -
3. Re: Composite Key / Foreign Key EJB3
lhoriman Jul 18, 2005 5:41 PM (in response to kostaboss)The @Id field is needed. There won't be extra DB columns.
The @Id field (composite primary key) is what creates the id columns for personA and personB.
The (insertable=false, updatable=false) on the Person fields in your Personrelationship, combined with the column name overlap with the PK, means that while Hibernate will create the associated objects, there is no extra pollution of the DB data model.
Make sense?
Just a nit... but it wouldn't hurt to follow java-standard capitalization conventions, especially when random people on message boards have to read your code.
Jeff Schnitzer
http://www.similarity.net -
4. Re: Composite Key / Foreign Key EJB3
kostaboss Jul 20, 2005 9:30 AM (in response to kostaboss)yes I'm sorry about violating the java-standard capitalization conventions, you are right about that ... I changed everything for that matter ... If you like i can repost (hopefully i did good)... I guess its not that important thou
thnx for your help again ... I had oversighted you putting the @Embendable in the PK class not in the "Person" - Entity. Works fine now. thnx a lot -
5. Re: Composite Key / Foreign Key EJB3
jorge_tello5 Aug 2, 2005 5:27 PM (in response to kostaboss)Hi,
My problem is:
I got an Entity RolProcTrans (pk is codProceso, codTrans,tipoRol).@Entity @Table(name = "ROL_PROC_TRANS") public class RolProcTrans implements Serializable { private RolProcTransPK pk; private RolTransaccion1 rolTransaccion1; @ManyToOne @JoinColumns( { @JoinColumn(name = "COD_TRANS", insertable = false, updatable = false), @JoinColumn(name = "TIPO_ROL", insertable = false, updatable = false) }) public RolTransaccion1 getRolTransaccion1() { return rolTransaccion1; } public void setRolTransaccion1(RolTransaccion1 rolTransaccion1) { this.rolTransaccion1 = rolTransaccion1; } @Id(generate=GeneratorType.NONE) public RolProcTransPK getPk() { return pk; } public void setPk(RolProcTransPK pk) { this.pk = pk; } }
the pk class is:@Embeddable public class RolProcTransPK implements Serializable { private Long codProceso; private String codTrans; private Integer tipoRol; ...
and Entity RolTransaccion1@Entity @Table(name = "ROL_TRANSACCION_1") public class RolTransaccion1 implements Serializable { private String codEmpresa; private RolTransaccion1PK pk; private List<RolProcTrans> rolesProcTrans; @OneToMany(fetch = FetchType.LAZY, mappedBy = "rolTransaccion1") public List<RolProcTrans> getRolesProcTrans() { return rolesProcTrans; } public void setRolesProcTrans(List<RolProcTrans> rolesProcTrans) { this.rolesProcTrans = rolesProcTrans; } @Column(name = "COD_EMPRESA") public String getCodEmpresa() { return codEmpresa; } public void setCodEmpresa(String codEmpresa) { this.codEmpresa = codEmpresa; } @Id(generate=GeneratorType.NONE) public RolTransaccion1PK getPk() { return pk; } public void setPk(RolTransaccion1PK pk) { this.pk = pk; } }
pk class@Embeddable public class RolTransaccion1PK implements Serializable { private String codTrans; private Integer tipoRol; ...
I´m use SQL Server 2000 SP3.
When I try list RolProcTrans Entity
the error is:
16:13:38,623 ERROR [JDBCExceptionReporter] [Microsoft][SQLServer 2000 Driver for JDBC]Value can not be converted to requested type.
I try, remove @ManyToOne relation and this function correctly
What Happend?
pleace some Idea.
Thank you very much