|
View:
New views
12 Messages
—
Rating Filter:
Alert me
|
|
|
Entity relationship issueI am having an issue with creating a relationship between 2 entities, where the parent entity was created from a Stored Procedure. When I try to use the @OneToMany annotation it tries to join the two entities.
@Entity public class CpUser extends BaseDomain { // Attributes @Id @Column(name = "paramUserID") private String userId; @Column(name = "paramInitials") private String initials; @Column(name = "paramFirstName") private String firstName; @Column(name = "paramLastName") private String lastName; ..... /* * List of user messages */ private Set<CpMsg> messages = new HashSet<CpMsg>(0); ..... @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "userId") public Set<CpMsg> getMessages() { return messages; } } // End CpUser @Entity @Table(name = "CP_MSG") public class CpMsg extends BaseDomain { // Fields @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "MSG_ID", unique = true, nullable = false, precision = 11, scale = 0) private Long msgId; @Column(name = "MSG_TEXT", nullable = false) private String msgText; private CpUser cpUser; .... public CpUser getCpUser() { return this.cpUser; } } //End CpMsg I removed some fields for brevity. When I try and get the messages for that user by calling getMessages() I get this in the logs - Call: SELECT t1.MSG_ID, t1.MSG_TEXT, t1.USER_ID FROM CPUSER_CP_MSG t0, CP_MSG t1 WHERE ((t0.CpUser_paramUserID = ?) AND (t1.MSG_ID = t0.messages_MSG_ID)) bind => [USER123] And the error is that CPUSER_CP_MSG doesnt exist, which is correct. I guess I assumed it would just do a select on the child table passing in the value from the parent. Thanks in advance |
|
|
Re: Entity relationship issueIt doesn't look like your relationship is defined in both entities. In
JPA you always need to make sure you do this. You need to have the @ManyToOne annotation back in your CpMsg entity. Check: http://en.wikibooks.org/wiki/Java_Persistence/OneToMany for more info. ./tch On Tue, Dec 2, 2008 at 6:20 PM, khaskett <khaskett@...> wrote: > > I am having an issue with creating a relationship between 2 entities, where > the parent entity was created from a Stored Procedure. When I try to use the > @OneToMany annotation it tries to join the two entities. > > @Entity > public class CpUser extends BaseDomain { > > // Attributes > @Id > @Column(name = "paramUserID") > private String userId; > > @Column(name = "paramInitials") > private String initials; > > @Column(name = "paramFirstName") > private String firstName; > > @Column(name = "paramLastName") > private String lastName; > > ..... > > /* > * List of user messages > */ > private Set<CpMsg> messages = new HashSet<CpMsg>(0); > > ..... > @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = > "userId") > public Set<CpMsg> getMessages() { > return messages; > } > } // End CpUser > > @Entity > @Table(name = "CP_MSG") > public class CpMsg extends BaseDomain { > > // Fields > @Id > @GeneratedValue(strategy = GenerationType.IDENTITY) > @Column(name = "MSG_ID", unique = true, nullable = false, precision = 11, > scale = 0) > private Long msgId; > @Column(name = "MSG_TEXT", nullable = false) > private String msgText; > > private CpUser cpUser; > .... > public CpUser getCpUser() { > return this.cpUser; > } > } //End CpMsg > > I removed some fields for brevity. > When I try and get the messages for that user by calling getMessages() I get > this in the logs - > Call: SELECT t1.MSG_ID, t1.MSG_TEXT, t1.USER_ID FROM CPUSER_CP_MSG t0, > CP_MSG t1 WHERE ((t0.CpUser_paramUserID = ?) AND (t1.MSG_ID = > t0.messages_MSG_ID)) > bind => [USER123] > > And the error is that CPUSER_CP_MSG doesnt exist, which is correct. > > I guess I assumed it would just do a select on the child table passing in > the value from the parent. > > Thanks in advance > > -- > View this message in context: http://www.nabble.com/Entity-relationship-issue-tp20803337p20803337.html > Sent from the EclipseLink - Users mailing list archive at Nabble.com. > > _______________________________________________ > eclipselink-users mailing list > eclipselink-users@... > https://dev.eclipse.org/mailman/listinfo/eclipselink-users > eclipselink-users mailing list eclipselink-users@... https://dev.eclipse.org/mailman/listinfo/eclipselink-users |
|
|
Re: Entity relationship issueI added the @ManyToOne and i still get the same error -
public class CpUser extends BaseDomain { .... @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "cpUser") @JoinColumn(referencedColumnName = "USER_ID") public Set<CpMsg> getMessages() { return messages; } public class CpMsg extends BaseDomain { .... @ManyToOne(fetch = FetchType.LAZY) public CpUser getCpUser() { return this.cpUser; } Thanks for the reply.
|
|
|
Re: Entity relationship issueYou forgot the mappedBy attribute in your @ManyToOne annotation it
should be @ManyToOne(fetch = FetchType.LAZY,mappedBy="messages") You might try to generate your Entities using the Dali JPA Tools in Eclipse to see the proper way to do these mappings. ./tch On Tue, Dec 2, 2008 at 11:59 PM, khaskett <khaskett@...> wrote: > > I added the @ManyToOne and i still get the same error - > > public class CpUser extends BaseDomain { > .... > @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = > "cpUser") > @JoinColumn(referencedColumnName = "USER_ID") > public Set<CpMsg> getMessages() { > return messages; > } > > public class CpMsg extends BaseDomain { > .... > @ManyToOne(fetch = FetchType.LAZY) > public CpUser getCpUser() { > return this.cpUser; > } > > Thanks for the reply. > > > Tim Hollosy wrote: >> >> It doesn't look like your relationship is defined in both entities. In >> JPA you always need to make sure you do this. You need to have the >> @ManyToOne annotation back in your CpMsg entity. >> >> Check: http://en.wikibooks.org/wiki/Java_Persistence/OneToMany >> >> for more info. >> ./tch >> >> >> >> On Tue, Dec 2, 2008 at 6:20 PM, khaskett <khaskett@...> wrote: >>> >>> I am having an issue with creating a relationship between 2 entities, >>> where >>> the parent entity was created from a Stored Procedure. When I try to use >>> the >>> @OneToMany annotation it tries to join the two entities. >>> >>> @Entity >>> public class CpUser extends BaseDomain { >>> >>> // Attributes >>> @Id >>> @Column(name = "paramUserID") >>> private String userId; >>> >>> @Column(name = "paramInitials") >>> private String initials; >>> >>> @Column(name = "paramFirstName") >>> private String firstName; >>> >>> @Column(name = "paramLastName") >>> private String lastName; >>> >>> ..... >>> >>> /* >>> * List of user messages >>> */ >>> private Set<CpMsg> messages = new HashSet<CpMsg>(0); >>> >>> ..... >>> @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy >>> = >>> "userId") >>> public Set<CpMsg> getMessages() { >>> return messages; >>> } >>> } // End CpUser >>> >>> @Entity >>> @Table(name = "CP_MSG") >>> public class CpMsg extends BaseDomain { >>> >>> // Fields >>> @Id >>> @GeneratedValue(strategy = GenerationType.IDENTITY) >>> @Column(name = "MSG_ID", unique = true, nullable = false, >>> precision = 11, >>> scale = 0) >>> private Long msgId; >>> @Column(name = "MSG_TEXT", nullable = false) >>> private String msgText; >>> >>> private CpUser cpUser; >>> .... >>> public CpUser getCpUser() { >>> return this.cpUser; >>> } >>> } //End CpMsg >>> >>> I removed some fields for brevity. >>> When I try and get the messages for that user by calling getMessages() I >>> get >>> this in the logs - >>> Call: SELECT t1.MSG_ID, t1.MSG_TEXT, t1.USER_ID FROM CPUSER_CP_MSG t0, >>> CP_MSG t1 WHERE ((t0.CpUser_paramUserID = ?) AND (t1.MSG_ID = >>> t0.messages_MSG_ID)) >>> bind => [USER123] >>> >>> And the error is that CPUSER_CP_MSG doesnt exist, which is correct. >>> >>> I guess I assumed it would just do a select on the child table passing in >>> the value from the parent. >>> >>> Thanks in advance >>> >>> -- >>> View this message in context: >>> http://www.nabble.com/Entity-relationship-issue-tp20803337p20803337.html >>> Sent from the EclipseLink - Users mailing list archive at Nabble.com. >>> >>> _______________________________________________ >>> eclipselink-users mailing list >>> eclipselink-users@... >>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users >>> >> _______________________________________________ >> eclipselink-users mailing list >> eclipselink-users@... >> https://dev.eclipse.org/mailman/listinfo/eclipselink-users >> >> > > -- > View this message in context: http://www.nabble.com/Entity-relationship-issue-tp20803337p20806849.html > Sent from the EclipseLink - Users mailing list archive at Nabble.com. > > _______________________________________________ > eclipselink-users mailing list > eclipselink-users@... > https://dev.eclipse.org/mailman/listinfo/eclipselink-users > eclipselink-users mailing list eclipselink-users@... https://dev.eclipse.org/mailman/listinfo/eclipselink-users |
|
|
|
|
|
Re: Entity relationship issueI added that and I still get this error -
[IBM][SQLServer JDBC Driver][SQLServer]Invalid object name 'CPUSER_CP_MSG'. Call: SELECT t1.MSG_ID, t1.MSG_TEXT, t1.CPUSER_paramUserID FROM CPUSER_CP_MSG t0, CP_MSG t1 WHERE ((t0.CpUser_paramUserID = ?) AND (t1.MSG_ID = t0.messages_MSG_ID)) bind => [USER123 ] I guess what I thought would happen is this call SELECT t1.MSG_ID, t1.MSG_TEXT, t1.CPUSER_paramUserID FROM CPUSER_CP_MSG t0, CP_MSG t1 WHERE t1.USER_ID = ? Thanks for reply. Kevin
|
|
|
Re: Entity relationship issueKhaskett,
Your JoinColumn is being specified on the wrong side of the relationship. It should be specified on the ManyToOne and not on the OneToMany in this case since it is the non-owning side (as specified by the mappedBy value). Cheers, Guy ----- Original Message ----- From: "Michael O'Brien" <michael.obrien@...> To: "EclipseLink User Discussions" <eclipselink-users@...>; "EclipseLink User Discussions" <eclipselink-users@...> Sent: Wednesday, December 03, 2008 7:59 AM Subject: Re: [eclipselink-users] Entity relationship issue Khaskett, Tim, My understanding of the mappedBy attribute is that only one (the inverse non-owning) side of a bidirectional relationship specifies the field on the owner of the relationship. Therefore the older version where there is a mappedBy only on the @OneToMany is correct (as long as CpMsg is the owner). These should be correct (without a mappedBy on the ManyToOne). @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "cpUser") @ManyToOne(fetch = FetchType.LAZY) To verify this I checked the EJB 3.0 persistence specification. See section 2.1.7 "Entity Relationships". There are two clauses... "The inverse side of a bidirectional relationship must refer to its owning side by use of the mappedBy element of the OneToOne, OneToMany or ManyToMany annotation". - Tim thank you for suggesting the mappedBy attribute. But, I noticed that the qualifying phrase left of ManyToOne. In the 2nd point it states that in the case of a 1-m and m-1 relationship that "the mappedBy element cannot be specified by the ManyToOne annotation. So it looks like the ManyToOne in the case must always be the owning side of the relationship - forcing only the OneToMany to have the mappedBy attribute. In my own experiments with JPA I was only able to move the single owner around in a ManyToMany relationship. See also the JEE5 tutorial that restates these two points about a single mappedBy and the "many side of a many-to-one" is not the owner under "Bidirectional Relationships". http://java.sun.com/javaee/5/docs/tutorial/doc/bnbqa.html Note 1: I suspect that your JoinColumn attribute may be causing conflict problems with the mappedBy attribute - try removing JoinColumn or using "name" instead of "referencedColumnName" within it - I think we will pick it up. I might be missing something but the ID and the JoinColumn names are different. Note 2: verify you want lazy loading on both sides of the relationship - I need to find out which 4 combinations are valid. Thank you /michael ----------Original Message---------- From: "Tim Hollosy" <hollosyt@...> Sent: Wed, December 03, 2008 6:05 AM To: "EclipseLink User Discussions" <eclipselink-users@...> Subject: Re: [eclipselink-users] Entity relationship issue You forgot the mappedBy attribute in your @ManyToOne annotation it should be @ManyToOne(fetch = FetchType.LAZY,mappedBy="messages") You might try to generate your Entities using the Dali JPA Tools in Eclipse to see the proper way to do these mappings. ./tch On Tue, Dec 2, 2008 at 11:59 PM, khaskett <khaskett@...> wrote: > > I added the @ManyToOne and i still get the same error - > > public class CpUser extends BaseDomain { > .... > @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy > = > "cpUser") > @JoinColumn(referencedColumnName = "USER_ID") > public Set<CpMsg> getMessages() { > return messages; > } > > public class CpMsg extends BaseDomain { > .... > @ManyToOne(fetch = FetchType.LAZY) > public CpUser getCpUser() { > return this.cpUser; > } > > Thanks for the reply. > > > Tim Hollosy wrote: >> >> It doesn't look like your relationship is defined in both entities. In >> JPA you always need to make sure you do this. You need to have the >> @ManyToOne annotation back in your CpMsg entity. >> >> Check: http://en.wikibooks.org/wiki/Java_Persistence/OneToMany >> >> for more info. >> ./tch >> >> >> >> On Tue, Dec 2, 2008 at 6:20 PM, khaskett <khaskett@...> wrote: >>> >>> I am having an issue with creating a relationship between 2 entities, >>> where >>> the parent entity was created from a Stored Procedure. When I try to use >>> the >>> @OneToMany annotation it tries to join the two entities. >>> >>> @Entity >>> public class CpUser extends BaseDomain { >>> >>> // Attributes >>> @Id >>> @Column(name = "paramUserID") >>> private String userId; >>> >>> @Column(name = "paramInitials") >>> private String initials; >>> >>> @Column(name = "paramFirstName") >>> private String firstName; >>> >>> @Column(name = "paramLastName") >>> private String lastName; >>> >>> ..... >>> >>> /* >>> * List of user messages >>> */ >>> private Set<CpMsg> messages = new HashSet<CpMsg>(0); >>> >>> ..... >>> @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, >>> mappedBy >>> = >>> "userId") >>> public Set<CpMsg> getMessages() { >>> return messages; >>> } >>> } // End CpUser >>> >>> @Entity >>> @Table(name = "CP_MSG") >>> public class CpMsg extends BaseDomain { >>> >>> // Fields >>> @Id >>> @GeneratedValue(strategy = GenerationType.IDENTITY) >>> @Column(name = "MSG_ID", unique = true, nullable = false, >>> precision = 11, >>> scale = 0) >>> private Long msgId; >>> @Column(name = "MSG_TEXT", nullable = false) >>> private String msgText; >>> >>> private CpUser cpUser; >>> .... >>> public CpUser getCpUser() { >>> return this.cpUser; >>> } >>> } //End CpMsg >>> >>> I removed some fields for brevity. >>> When I try and get the messages for that user by calling getMessages() I >>> get >>> this in the logs - >>> Call: SELECT t1.MSG_ID, t1.MSG_TEXT, t1.USER_ID FROM CPUSER_CP_MSG t0, >>> CP_MSG t1 WHERE ((t0.CpUser_paramUserID = ?) AND (t1.MSG_ID = >>> t0.messages_MSG_ID)) >>> bind => [USER123] >>> >>> And the error is that CPUSER_CP_MSG doesnt exist, which is correct. >>> >>> I guess I assumed it would just do a select on the child table passing >>> in >>> the value from the parent. >>> >>> Thanks in advance >>> >>> -- >>> View this message in context: >>> http://www.nabble.com/Entity-relationship-issue-tp20803337p20803337.html >>> Sent from the EclipseLink - Users mailing list archive at Nabble.com. >>> >>> _______________________________________________ >>> eclipselink-users mailing list >>> eclipselink-users@... >>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users >>> >> _______________________________________________ >> eclipselink-users mailing list >> eclipselink-users@... >> https://dev.eclipse.org/mailman/listinfo/eclipselink-users >> >> > > -- > View this message in context: > http://www.nabble.com/Entity-relationship-issue-tp20803337p20806849.html > Sent from the EclipseLink - Users mailing list archive at Nabble.com. > > _______________________________________________ > eclipselink-users mailing list > eclipselink-users@... > https://dev.eclipse.org/mailman/listinfo/eclipselink-users > eclipselink-users mailing list eclipselink-users@... https://dev.eclipse.org/mailman/listinfo/eclipselink-users _______________________________________________ eclipselink-users mailing list eclipselink-users@... https://dev.eclipse.org/mailman/listinfo/eclipselink-users _______________________________________________ eclipselink-users mailing list eclipselink-users@... https://dev.eclipse.org/mailman/listinfo/eclipselink-users |
|
|
Re: Entity relationship issueKevin,
What does your model look like exactly to get this? Are you purposely trying to use a uni-directional 1-M using a JoinTable? Because what I am seeing would indicate that there is no mappedBy value specified on the 1-M. Cheers, Guy ----- Original Message ----- From: "khaskett" <khaskett@...> To: <eclipselink-users@...> Sent: Wednesday, December 03, 2008 8:49 AM Subject: Re: [eclipselink-users] Entity relationship issue > > I added that and I still get this error - > [IBM][SQLServer JDBC Driver][SQLServer]Invalid object name > 'CPUSER_CP_MSG'. > Call: SELECT t1.MSG_ID, t1.MSG_TEXT, t1.CPUSER_paramUserID FROM > CPUSER_CP_MSG t0, CP_MSG t1 WHERE ((t0.CpUser_paramUserID = ?) AND > (t1.MSG_ID = t0.messages_MSG_ID)) > bind => [USER123 ] > > I guess what I thought would happen is this call > SELECT t1.MSG_ID, t1.MSG_TEXT, t1.CPUSER_paramUserID FROM CPUSER_CP_MSG > t0, > CP_MSG t1 WHERE t1.USER_ID = ? > > Thanks for reply. > Kevin > > > Tim Hollosy wrote: >> >> You forgot the mappedBy attribute in your @ManyToOne annotation it >> should be @ManyToOne(fetch = FetchType.LAZY,mappedBy="messages") >> >> You might try to generate your Entities using the Dali JPA Tools in >> Eclipse to see the proper way to do these mappings. >> >> ./tch >> >> >> >> On Tue, Dec 2, 2008 at 11:59 PM, khaskett <khaskett@...> wrote: >>> >>> I added the @ManyToOne and i still get the same error - >>> >>> public class CpUser extends BaseDomain { >>> .... >>> @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, >>> mappedBy >>> = >>> "cpUser") >>> @JoinColumn(referencedColumnName = "USER_ID") >>> public Set<CpMsg> getMessages() { >>> return messages; >>> } >>> >>> public class CpMsg extends BaseDomain { >>> .... >>> @ManyToOne(fetch = FetchType.LAZY) >>> public CpUser getCpUser() { >>> return this.cpUser; >>> } >>> >>> Thanks for the reply. >>> >>> >>> Tim Hollosy wrote: >>>> >>>> It doesn't look like your relationship is defined in both entities. In >>>> JPA you always need to make sure you do this. You need to have the >>>> @ManyToOne annotation back in your CpMsg entity. >>>> >>>> Check: http://en.wikibooks.org/wiki/Java_Persistence/OneToMany >>>> >>>> for more info. >>>> ./tch >>>> >>>> >>>> >>>> On Tue, Dec 2, 2008 at 6:20 PM, khaskett <khaskett@...> wrote: >>>>> >>>>> I am having an issue with creating a relationship between 2 entities, >>>>> where >>>>> the parent entity was created from a Stored Procedure. When I try to >>>>> use >>>>> the >>>>> @OneToMany annotation it tries to join the two entities. >>>>> >>>>> @Entity >>>>> public class CpUser extends BaseDomain { >>>>> >>>>> // Attributes >>>>> @Id >>>>> @Column(name = "paramUserID") >>>>> private String userId; >>>>> >>>>> @Column(name = "paramInitials") >>>>> private String initials; >>>>> >>>>> @Column(name = "paramFirstName") >>>>> private String firstName; >>>>> >>>>> @Column(name = "paramLastName") >>>>> private String lastName; >>>>> >>>>> ..... >>>>> >>>>> /* >>>>> * List of user messages >>>>> */ >>>>> private Set<CpMsg> messages = new HashSet<CpMsg>(0); >>>>> >>>>> ..... >>>>> @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, >>>>> mappedBy >>>>> = >>>>> "userId") >>>>> public Set<CpMsg> getMessages() { >>>>> return messages; >>>>> } >>>>> } // End CpUser >>>>> >>>>> @Entity >>>>> @Table(name = "CP_MSG") >>>>> public class CpMsg extends BaseDomain { >>>>> >>>>> // Fields >>>>> @Id >>>>> @GeneratedValue(strategy = GenerationType.IDENTITY) >>>>> @Column(name = "MSG_ID", unique = true, nullable = false, >>>>> precision = 11, >>>>> scale = 0) >>>>> private Long msgId; >>>>> @Column(name = "MSG_TEXT", nullable = false) >>>>> private String msgText; >>>>> >>>>> private CpUser cpUser; >>>>> .... >>>>> public CpUser getCpUser() { >>>>> return this.cpUser; >>>>> } >>>>> } //End CpMsg >>>>> >>>>> I removed some fields for brevity. >>>>> When I try and get the messages for that user by calling getMessages() >>>>> I >>>>> get >>>>> this in the logs - >>>>> Call: SELECT t1.MSG_ID, t1.MSG_TEXT, t1.USER_ID FROM CPUSER_CP_MSG t0, >>>>> CP_MSG t1 WHERE ((t0.CpUser_paramUserID = ?) AND (t1.MSG_ID = >>>>> t0.messages_MSG_ID)) >>>>> bind => [USER123] >>>>> >>>>> And the error is that CPUSER_CP_MSG doesnt exist, which is correct. >>>>> >>>>> I guess I assumed it would just do a select on the child table passing >>>>> in >>>>> the value from the parent. >>>>> >>>>> Thanks in advance >>>>> >>>>> -- >>>>> View this message in context: >>>>> http://www.nabble.com/Entity-relationship-issue-tp20803337p20803337.html >>>>> Sent from the EclipseLink - Users mailing list archive at Nabble.com. >>>>> >>>>> _______________________________________________ >>>>> eclipselink-users mailing list >>>>> eclipselink-users@... >>>>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users >>>>> >>>> _______________________________________________ >>>> eclipselink-users mailing list >>>> eclipselink-users@... >>>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users >>>> >>>> >>> >>> -- >>> View this message in context: >>> http://www.nabble.com/Entity-relationship-issue-tp20803337p20806849.html >>> Sent from the EclipseLink - Users mailing list archive at Nabble.com. >>> >>> _______________________________________________ >>> eclipselink-users mailing list >>> eclipselink-users@... >>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users >>> >> _______________________________________________ >> eclipselink-users mailing list >> eclipselink-users@... >> https://dev.eclipse.org/mailman/listinfo/eclipselink-users >> >> > > -- > View this message in context: > http://www.nabble.com/Entity-relationship-issue-tp20803337p20813256.html > Sent from the EclipseLink - Users mailing list archive at Nabble.com. > > _______________________________________________ > eclipselink-users mailing list > eclipselink-users@... > https://dev.eclipse.org/mailman/listinfo/eclipselink-users > _______________________________________________ eclipselink-users mailing list eclipselink-users@... https://dev.eclipse.org/mailman/listinfo/eclipselink-users |
|
|
Re: Entity relationship issueHello,
There have been a number of Responses so far, but I wanted to point out you have annotations on both fields and properties which is not allowed. Putting the @OneToMany on the getMessages is likely causing it to be ignored - which is why it is using the CPUSER_CP_MSG table, since it thinks this is a unidirectional 1:M relation. Best Regards, Chris khaskett wrote: > I am having an issue with creating a relationship between 2 entities, where > the parent entity was created from a Stored Procedure. When I try to use the > @OneToMany annotation it tries to join the two entities. > > @Entity > public class CpUser extends BaseDomain { > > // Attributes > @Id > @Column(name = "paramUserID") > private String userId; > > @Column(name = "paramInitials") > private String initials; > > @Column(name = "paramFirstName") > private String firstName; > > @Column(name = "paramLastName") > private String lastName; > > ..... > > /* > * List of user messages > */ > private Set<CpMsg> messages = new HashSet<CpMsg>(0); > > ..... > @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = > "userId") > public Set<CpMsg> getMessages() { > return messages; > } > } // End CpUser > > @Entity > @Table(name = "CP_MSG") > public class CpMsg extends BaseDomain { > > // Fields > @Id > @GeneratedValue(strategy = GenerationType.IDENTITY) > @Column(name = "MSG_ID", unique = true, nullable = false, precision = 11, > scale = 0) > private Long msgId; > @Column(name = "MSG_TEXT", nullable = false) > private String msgText; > > private CpUser cpUser; > .... > public CpUser getCpUser() { > return this.cpUser; > } > } //End CpMsg > > I removed some fields for brevity. > When I try and get the messages for that user by calling getMessages() I get > this in the logs - > Call: SELECT t1.MSG_ID, t1.MSG_TEXT, t1.USER_ID FROM CPUSER_CP_MSG t0, > CP_MSG t1 WHERE ((t0.CpUser_paramUserID = ?) AND (t1.MSG_ID = > t0.messages_MSG_ID)) > bind => [USER123] > > And the error is that CPUSER_CP_MSG doesnt exist, which is correct. > > I guess I assumed it would just do a select on the child table passing in > the value from the parent. > > Thanks in advance > > eclipselink-users mailing list eclipselink-users@... https://dev.eclipse.org/mailman/listinfo/eclipselink-users |
|
|
Re: Entity relationship issueThe CpUser entity is the parent and the CpMsg object is the child in the one to many relationship.
CpUser is populated using a call to a stored procedure in a SQL Server DB CpMsg object represents this table in a AS400 db - CP_MSG ( MSG_ID decimal(11,0) PRIMARY KEY NOT NULL, POLICY_NBR varchar(20), USER_ID varchar(20) NOT NULL, MSG_TIME timestamp NOT NULL, MSG_TYPE varchar(5), MSG_TEXT varchar(255) NOT NULL ) The userid in CpUser and the USER_ID in CpMsg is what links the CpUser to the CpMsg entities. Thanks for the reply. Kevin
|
|
|
Re: Entity relationship issueOk so couple things:
1 - As Chris pointed out it looks like you are mixing access types which is not allowed (which explains the defaulting of the uni 1-M using a JoinTable) 2 - As I mentioned previously, your 1-M should specify the mappedBy value and the JoinColumn(name="USER_ID") should be specified on your M-1. Cheers, Guy ----- Original Message ----- From: "khaskett" <khaskett@...> To: <eclipselink-users@...> Sent: Wednesday, December 03, 2008 10:02 AM Subject: Re: [eclipselink-users] Entity relationship issue > > The CpUser entity is the parent and the CpMsg object is the child in the > one > to many relationship. > CpUser is populated using a call to a stored procedure in a SQL Server DB > > CpMsg object represents this table in a AS400 db - > CP_MSG > ( > MSG_ID decimal(11,0) PRIMARY KEY NOT NULL, > POLICY_NBR varchar(20), > USER_ID varchar(20) NOT NULL, > MSG_TIME timestamp NOT NULL, > MSG_TYPE varchar(5), > MSG_TEXT varchar(255) NOT NULL > ) > > The userid in CpUser and the USER_ID in CpMsg is what links the CpUser to > the CpMsg entities. > > Thanks for the reply. > Kevin > > > > Guy Pelletier wrote: >> >> Kevin, >> >> What does your model look like exactly to get this? Are you purposely >> trying >> to use a uni-directional 1-M using a JoinTable? Because what I am seeing >> would indicate that there is no mappedBy value specified on the 1-M. >> >> Cheers, >> Guy >> >> ----- Original Message ----- >> From: "khaskett" <khaskett@...> >> To: <eclipselink-users@...> >> Sent: Wednesday, December 03, 2008 8:49 AM >> Subject: Re: [eclipselink-users] Entity relationship issue >> >> >>> >>> I added that and I still get this error - >>> [IBM][SQLServer JDBC Driver][SQLServer]Invalid object name >>> 'CPUSER_CP_MSG'. >>> Call: SELECT t1.MSG_ID, t1.MSG_TEXT, t1.CPUSER_paramUserID FROM >>> CPUSER_CP_MSG t0, CP_MSG t1 WHERE ((t0.CpUser_paramUserID = ?) AND >>> (t1.MSG_ID = t0.messages_MSG_ID)) >>> bind => [USER123 ] >>> >>> I guess what I thought would happen is this call >>> SELECT t1.MSG_ID, t1.MSG_TEXT, t1.CPUSER_paramUserID FROM CPUSER_CP_MSG >>> t0, >>> CP_MSG t1 WHERE t1.USER_ID = ? >>> >>> Thanks for reply. >>> Kevin >>> >>> >>> Tim Hollosy wrote: >>>> >>>> You forgot the mappedBy attribute in your @ManyToOne annotation it >>>> should be @ManyToOne(fetch = FetchType.LAZY,mappedBy="messages") >>>> >>>> You might try to generate your Entities using the Dali JPA Tools in >>>> Eclipse to see the proper way to do these mappings. >>>> >>>> ./tch >>>> >>>> >>>> >>>> On Tue, Dec 2, 2008 at 11:59 PM, khaskett <khaskett@...> wrote: >>>>> >>>>> I added the @ManyToOne and i still get the same error - >>>>> >>>>> public class CpUser extends BaseDomain { >>>>> .... >>>>> @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, >>>>> mappedBy >>>>> = >>>>> "cpUser") >>>>> @JoinColumn(referencedColumnName = "USER_ID") >>>>> public Set<CpMsg> getMessages() { >>>>> return messages; >>>>> } >>>>> >>>>> public class CpMsg extends BaseDomain { >>>>> .... >>>>> @ManyToOne(fetch = FetchType.LAZY) >>>>> public CpUser getCpUser() { >>>>> return this.cpUser; >>>>> } >>>>> >>>>> Thanks for the reply. >>>>> >>>>> >>>>> Tim Hollosy wrote: >>>>>> >>>>>> It doesn't look like your relationship is defined in both entities. >>>>>> In >>>>>> JPA you always need to make sure you do this. You need to have the >>>>>> @ManyToOne annotation back in your CpMsg entity. >>>>>> >>>>>> Check: http://en.wikibooks.org/wiki/Java_Persistence/OneToMany >>>>>> >>>>>> for more info. >>>>>> ./tch >>>>>> >>>>>> >>>>>> >>>>>> On Tue, Dec 2, 2008 at 6:20 PM, khaskett <khaskett@...> >>>>>> wrote: >>>>>>> >>>>>>> I am having an issue with creating a relationship between 2 >>>>>>> entities, >>>>>>> where >>>>>>> the parent entity was created from a Stored Procedure. When I try to >>>>>>> use >>>>>>> the >>>>>>> @OneToMany annotation it tries to join the two entities. >>>>>>> >>>>>>> @Entity >>>>>>> public class CpUser extends BaseDomain { >>>>>>> >>>>>>> // Attributes >>>>>>> @Id >>>>>>> @Column(name = "paramUserID") >>>>>>> private String userId; >>>>>>> >>>>>>> @Column(name = "paramInitials") >>>>>>> private String initials; >>>>>>> >>>>>>> @Column(name = "paramFirstName") >>>>>>> private String firstName; >>>>>>> >>>>>>> @Column(name = "paramLastName") >>>>>>> private String lastName; >>>>>>> >>>>>>> ..... >>>>>>> >>>>>>> /* >>>>>>> * List of user messages >>>>>>> */ >>>>>>> private Set<CpMsg> messages = new HashSet<CpMsg>(0); >>>>>>> >>>>>>> ..... >>>>>>> @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, >>>>>>> mappedBy >>>>>>> = >>>>>>> "userId") >>>>>>> public Set<CpMsg> getMessages() { >>>>>>> return messages; >>>>>>> } >>>>>>> } // End CpUser >>>>>>> >>>>>>> @Entity >>>>>>> @Table(name = "CP_MSG") >>>>>>> public class CpMsg extends BaseDomain { >>>>>>> >>>>>>> // Fields >>>>>>> @Id >>>>>>> @GeneratedValue(strategy = GenerationType.IDENTITY) >>>>>>> @Column(name = "MSG_ID", unique = true, nullable = false, >>>>>>> precision = 11, >>>>>>> scale = 0) >>>>>>> private Long msgId; >>>>>>> @Column(name = "MSG_TEXT", nullable = false) >>>>>>> private String msgText; >>>>>>> >>>>>>> private CpUser cpUser; >>>>>>> .... >>>>>>> public CpUser getCpUser() { >>>>>>> return this.cpUser; >>>>>>> } >>>>>>> } //End CpMsg >>>>>>> >>>>>>> I removed some fields for brevity. >>>>>>> When I try and get the messages for that user by calling >>>>>>> getMessages() >>>>>>> I >>>>>>> get >>>>>>> this in the logs - >>>>>>> Call: SELECT t1.MSG_ID, t1.MSG_TEXT, t1.USER_ID FROM CPUSER_CP_MSG >>>>>>> t0, >>>>>>> CP_MSG t1 WHERE ((t0.CpUser_paramUserID = ?) AND (t1.MSG_ID = >>>>>>> t0.messages_MSG_ID)) >>>>>>> bind => [USER123] >>>>>>> >>>>>>> And the error is that CPUSER_CP_MSG doesnt exist, which is correct. >>>>>>> >>>>>>> I guess I assumed it would just do a select on the child table >>>>>>> passing >>>>>>> in >>>>>>> the value from the parent. >>>>>>> >>>>>>> Thanks in advance >>>>>>> >>>>>>> -- >>>>>>> View this message in context: >>>>>>> http://www.nabble.com/Entity-relationship-issue-tp20803337p20803337.html >>>>>>> Sent from the EclipseLink - Users mailing list archive at >>>>>>> Nabble.com. >>>>>>> >>>>>>> _______________________________________________ >>>>>>> eclipselink-users mailing list >>>>>>> eclipselink-users@... >>>>>>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users >>>>>>> >>>>>> _______________________________________________ >>>>>> eclipselink-users mailing list >>>>>> eclipselink-users@... >>>>>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users >>>>>> >>>>>> >>>>> >>>>> -- >>>>> View this message in context: >>>>> http://www.nabble.com/Entity-relationship-issue-tp20803337p20806849.html >>>>> Sent from the EclipseLink - Users mailing list archive at Nabble.com. >>>>> >>>>> _______________________________________________ >>>>> eclipselink-users mailing list >>>>> eclipselink-users@... >>>>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users >>>>> >>>> _______________________________________________ >>>> eclipselink-users mailing list >>>> eclipselink-users@... >>>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users >>>> >>>> >>> >>> -- >>> View this message in context: >>> http://www.nabble.com/Entity-relationship-issue-tp20803337p20813256.html >>> Sent from the EclipseLink - Users mailing list archive at Nabble.com. >>> >>> _______________________________________________ >>> eclipselink-users mailing list >>> eclipselink-users@... >>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users >>> >> >> _______________________________________________ >> eclipselink-users mailing list >> eclipselink-users@... >> https://dev.eclipse.org/mailman/listinfo/eclipselink-users >> >> > > -- > View this message in context: > http://www.nabble.com/Entity-relationship-issue-tp20803337p20814607.html > Sent from the EclipseLink - Users mailing list archive at Nabble.com. > > _______________________________________________ > eclipselink-users mailing list > eclipselink-users@... > https://dev.eclipse.org/mailman/listinfo/eclipselink-users > _______________________________________________ eclipselink-users mailing list eclipselink-users@... https://dev.eclipse.org/mailman/listinfo/eclipselink-users |
|
|
Re: Entity relationship issueOk, I changed the annotations to both be on the getters for both entities and the SQL it is executing looks correct. But the problem now is that it is trying to execute it in the wrong PersistenceUnit I have defined for the CpMsg entity.
[IBM][SQLServer JDBC Driver][SQLServer]Invalid object name 'CP_MSG'. Error Code: 208 Call: SELECT MSG_ID, POLICY_NBR, MSG_TIME, MSG_TYPE, MSG_TEXT, USER_ID FROM CP_MSG WHERE (USER_ID = ?) bind => [USER123 ] CpUser is in a Persistence Unit for SQL server, and the CpMsg entity is in a PU that is for DB2. Is this not possible? Thanks again, Kevin
|
| Free embeddable forum powered by Nabble | Forum Help |