Return-Path: X-Original-To: apmail-openjpa-users-archive@minotaur.apache.org Delivered-To: apmail-openjpa-users-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 2E9311032E for ; Mon, 23 Jun 2014 13:24:43 +0000 (UTC) Received: (qmail 14807 invoked by uid 500); 23 Jun 2014 13:24:43 -0000 Delivered-To: apmail-openjpa-users-archive@openjpa.apache.org Received: (qmail 14763 invoked by uid 500); 23 Jun 2014 13:24:43 -0000 Mailing-List: contact users-help@openjpa.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@openjpa.apache.org Delivered-To: mailing list users@openjpa.apache.org Received: (qmail 14751 invoked by uid 99); 23 Jun 2014 13:24:42 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 23 Jun 2014 13:24:42 +0000 X-ASF-Spam-Status: No, hits=2.2 required=5.0 tests=HTML_MESSAGE,RCVD_IN_DNSWL_NONE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of mansour.alakeel@gmail.com designates 209.85.192.52 as permitted sender) Received: from [209.85.192.52] (HELO mail-qg0-f52.google.com) (209.85.192.52) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 23 Jun 2014 13:24:39 +0000 Received: by mail-qg0-f52.google.com with SMTP id f51so5940240qge.39 for ; Mon, 23 Jun 2014 06:24:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=qiE/NMj1ycYt+qvuBPNVrrzYsHNMDFAuwwbhrGA35i8=; b=VzlOElCo0eL9hXCeqzwLpuEYx3uvlcQkgMea2sbbqE1NEl6SyzCL7TlssLsV/0NE+9 RVCnUgrUfkpy/udNdP3c+MOLRBQUidIU5iS8iRLZHCiQH7MHoZVe+bsxusTsiwuWNjR8 yaUnavjtihyNryW9nJikParuSo6E/mMrvdYMS4UavGVHmTQUG6/7QQesU/kSnXzCUHG+ Hqe2PBqVry82Q7oJ3Z9LHELTiN8/utiw+BFN0E061rFq4CYfgzetheF20jVhdJoRdliY MCwNZqmx2IVW5Wm5CYMusGC5lB3Kce8ZvUrBEept5A8OenughHEboPpN7Xp5rW7I48ok 8SiA== MIME-Version: 1.0 X-Received: by 10.224.3.68 with SMTP id 4mr33309762qam.24.1403529855362; Mon, 23 Jun 2014 06:24:15 -0700 (PDT) Received: by 10.96.80.38 with HTTP; Mon, 23 Jun 2014 06:24:15 -0700 (PDT) In-Reply-To: <88CB62524DE0644AAE21D8BAB8CFC0CA0BC2C8A1@dueexs06.bertschi.domain> References: <88CB62524DE0644AAE21D8BAB8CFC0CA0BC2C8A1@dueexs06.bertschi.domain> Date: Mon, 23 Jun 2014 09:24:15 -0400 Message-ID: Subject: Re: How to map an ID of a foreign key as a field From: Mansour Al Akeel To: users Content-Type: multipart/alternative; boundary=001a11c24c8e76069204fc80c248 X-Virus-Checked: Checked by ClamAV on apache.org --001a11c24c8e76069204fc80c248 Content-Type: text/plain; charset=ISO-8859-1 John, the classes are not in the same persistence unit. So even getReference will not work. Thank you. On Mon, Jun 23, 2014 at 7:49 AM, Boblitz John wrote: > > -----Original Message----- > > From: Mansour Al Akeel [mailto:mansour.alakeel@gmail.com] > > Sent: Montag, 23. Juni 2014 10:23 > > To: users > > Subject: How to map an ID of a foreign key as a field > > > > I am looking to map a field in the owner entity to the ID of the target > entity. > > For example, taking the Employee -> Department mapping: > > > > class Employee { > > > > private String name ; > > > > private String depId ; > > > > private Department department ; > > > > @ManyToOne(fetch = FetchType.EAGER, optional = false) > > @JoinColumn(name = "dep_id", referencedColumnName = "id", insertable > > = false, updatable = false) public Department getDepartment(){ return > > this.department ; } > > > > > > public void setDepId(String depId){ > > this.depId = depId; > > } > > ... > > > > } > > > > My Question is, how can I set the Deparment by setting only its dep_id > in the > > employee object, and keep the foreign key constraints when the tables are > > created ?? > > > > For example: > > > > Employee emp = new Employee() ; > > emp.setId("SOME_ID_1") ; > > emp.setDepId("FINANCE"); > > > > em.persist(emp); > > > > and if there's no FINANCE record in the department table, then we have an > > constraints violation. > > > > At the same time I don't want to have setters for Department Entity. > Only its > > ID. > > > > Thank you. > > > > Hello Mansour, > > Not sure why you would need the extra Emplyoee.depId. > > I would do what you would like this way: > > Employee emp = new Employee() ; > emp.setId("SOME_ID_1") ; > emp.setDepartment(em.getReference(Employee.class, "FINANCE"); > em.persist(emp); > > > From the JavaDoc: > T getReference(java.lang.Class entityClass, java.lang.Object > primaryKey) > Get an instance, whose state may be lazily fetched. If the requested > instance does not exist in the database, the EntityNotFoundException is > thrown when the instance state is first accessed. (The persistence provider > runtime is permitted to throw the EntityNotFoundException when getReference > is called.) The application should not expect that the instance state will > be available upon detachment, unless it was accessed by the application > while the entity manager was open. > > Hope this helps. > > > John > --001a11c24c8e76069204fc80c248--