tag:blogger.com,1999:blog-20547117.post113996728648543526..comments2023-10-16T11:03:40.375+01:00Comments on A Programmer's Blog: Sharing attributes across EntitiesDibyendu Majumdarhttp://www.blogger.com/profile/08417788730731238290noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-20547117.post-1145173901373711502006-04-16T08:51:00.000+01:002006-04-16T08:51:00.000+01:00Ok, I'm happier now. I was under the mistaken impr...Ok, I'm happier now. I was under the mistaken impression that all @(DBRelation) annotations required an owner and a child. You can do it with JUST an owner, e.g. define the table:<BR/><BR/>Customer<BR/>id INT PK<BR/>name VarChar<BR/>addr1 INT<BR/>addr2 INT<BR/>--<BR/>Address<BR/>id INT PK<BR/>street1<BR/>street2<BR/>zip<BR/><BR/>Define the java:<BR/>@Entity<BR/> @Table(name = "address")<BR/>public class Addy {<BR/> Integer id;<BR/> String street1;<BR/> String street2;<BR/> public Addy() { }<BR/> @Id<BR/> public Integer getId() {<BR/> return id;<BR/> } // setters and getters for street1, street2. Thats it.<BR/>}<BR/><BR/>Define the Customer class:<BR/><BR/>@Entity<BR/> @Table(name = "customer")<BR/> public class Customer {<BR/> private int id;<BR/> private String name;<BR/> private Addy addr1;<BR/> private Addy addr2;<BR/> @Id<BR/> public int getId() {<BR/> return id;<BR/> }<BR/> @OneToOne()<BR/> @JoinColumn(name = "addr1")<BR/> public Addy getAddr1() {<BR/> return addy1;<BR/> }<BR/> @OneToOne()<BR/> @JoinColumn(name = "addr2")<BR/> public Addy getAddr2() {<BR/> return addy2;<BR/> }<BR/>// the rest of getters and setters; no more annotation needed<BR/>}<BR/><BR/><BR/>I post this back mostly on the thought that, since I googled too here, someone else might :~).<BR/><BR/>Thanks for your response, it is much appreciated.<BR/><BR/>For the random visitor who cares to check: I *might* put up a write up on the pain of the first week of JSF with resource injection on my site; it's worth a click anyway. If I make one, it will be obvious on the main page.<BR/><BR/>I'm sure I'll need to reference it when I need to start a new site ;~)<BR/><BR/>Cheers,<BR/>GeoffAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-20547117.post-1145134450807520632006-04-15T21:54:00.000+01:002006-04-15T21:54:00.000+01:00Hello Geoff,If I understand you correctly, you are...Hello Geoff,<BR/><BR/>If I understand you correctly, you are storing addresses in a separate table. I think that this is fine.<BR/><BR/>In this case, you should probably use regular join semantics. That is, embed an Address object in your class, provide getter/setter for it, and then annotate the join condition.<BR/><BR/>Address should be another Entity.<BR/><BR/>For example:<BR/>@Entity<BR/>class Container {<BR/> Address address;<BR/> @ManyToOne(..)<BR/> public Address getAddress() {}<BR/>}<BR/><BR/>@Entity <BR/>class Address {<BR/>}<BR/><BR/>I hope this helps.<BR/><BR/>RegardsDibyendu Majumdarhttps://www.blogger.com/profile/08417788730731238290noreply@blogger.comtag:blogger.com,1999:blog-20547117.post-1145115145100483692006-04-15T16:32:00.000+01:002006-04-15T16:32:00.000+01:00A lot of wonderful tips in your blog, you've alrea...A lot of wonderful tips in your blog, you've already helped quite a bit in my little quest to figure out what I am doing. Maybe you would be so kind as to answer a question for me :~)<BR/><BR/>I am getting the impression that my DB design is bad: basically I am using pointers to an address column and pretending at some sick, twisted level that a database can be sort of object oriented.<BR/><BR/>So, a tbl_workSite Table has a whole bunch of information about a job site, and a site_address field which is an int, and points to the PK on tbl_Address.<BR/><BR/>I also have an 'owner_address' field in tbl_workSite which also points to the PK of tbl_Address.<BR/><BR/>How in the world to connect them? Embedded looks like it would work great... but I cannot find in the spec where it allows for the use of the @Table annotation. In fact, the spec states 'Each of the persistent properties or fields of the<BR/>embedded object is mapped to the database table for the entity'<BR/><BR/><BR/>Of course it isn't shockingly hard to do a manual query and grab the object on my own. But man it is pretty to not have to. To the point where I will gladly rebuild the db schema, as it is a work in progress anyway. I just LIKE having a nice, abstract 'Address' table. Every bloody thing I work with has an address. Most of them have two! Some have three! But the impression I am getting is that doing so is bad DB design. I can see, logically, why. But I am happy to kill a tiny bit of efficiency to get a whole lot of sanity... <BR/>Says the man using 4 different programs to build JSF pages. (Dreamweaver, Textpad, JBuilder and DBManager)<BR/><BR/>Anyway :~)<BR/>If you answer the question on your blog I will see it, or my e-mail addy is geoff_9@granum.biz. And if you don't have time, I'm sure I'll figure it out eventually ;~)<BR/><BR/>Thanks for the tips you've got up!<BR/><BR/>Cheers,<BR/>GeoffAnonymousnoreply@blogger.com