Foreign Key « Key « JPA Q&A

15.Data Type
54.Sql Server
55.Stored Procedure
JPA Q&A » Key » Foreign Key 

1. hibernate use two foreign keys for different coloumns?

is it possible to map two foreign keys linked to the same table? its best i show you what i mean. i have a table called fixtures which currently looks like this:

<class ...

2. Foreign Keys w/ JPA

I am using mysql as the database w/ EclipseLink. I have two classes, one with a @OneToMany and another with @ManyToOne annotations. A join table is created, but it doesn't have ...

3. How to do hibernate anotation for foreign key

I have below related tables

FLD_REF_LIST_ID varchar2(10) PK
....the rest of the columns....

FLD_DOCUMENT_ID varchar2(10) PK
....the rest of the columns....
FLD_PARENT_ID varchar2(10) 

....the rest of the columns....
FLD_DOCNUM varchar2(10) 


4. Hibernate throws WrongClassException for a simple foreign key

I have the following model: Contract has a set of ContractItem elements and each item contains further reference to other tables (type, group), so does the Contract (status, type). All of the mentioned ...

5. EJB3NamingStrategy vs ImprovedNamingStrategy Foreign Key Naming

My naming strategy in hibernate was the default one : EJB3NamingStrategy. So the field and table names was in camelcase. I switched it to ImprovedNamingStrategy to have snakecase. But my foreign keys ...

6. Hibernate foreign key

Dear All, I am not strong in Database concepts, I am having two tables, User and Salary In User table UserID is my primary key and in Salary table UserID and Salary_Date are composite keys. How can i set UserId as foreign key in Salary table? Can i use cascade on delete through Salry mapping file? Please help me out if ...

7. Hibernate - Foreign Keys

I noticed that if I generate my DB schema with will name all the foreign keys with generated meaningless names such as "FK87627BFE12D20136". I don't think I like it. 1) is there a way to have my own meaningful names ? 2) if I handcraft my own FK referential integrity into the DB... can I generate the FK from DB ...

8. hibernate disable foreign keys

I need to disable the creation of foreign keys when update is set, cause when i delete the FK and aplication restart, hibernate create all the FK again. When i was using MySql, hibernate does not create the FK contraints, but when i switch to Microsoft SQL server, it create the constraints but i need to delete it cause im ...

9. Strange Hibernate/foreign key problem

I'm having a strange foreign key problem. I'm allowing Hibernate to create my table structure based on my annotated classes. When using the following @Entity @Table(name="PERSON_CAR", schema="DBUSER" ) public class PersonCar { @Id @GeneratedValue(strategy=GenerationType.AUTO) public int getId() { return id; } @OneToOne public Car getCar() { return car; } ... The generated table includes: Table: Person_Car Columns: ID CAR_ID I'm getting ...

10. Hibernate Foreign Keys

Hi Guys, Here is my HBM files I am using ...

11. Foreign key in hibernate

Hi All, I am having two tables 1)project 2)Phase Project table contains: projectId projectName startDate endDate Phase table contains : projectId as foregin key phaseId as primary key phaseName startDate endDate this is one to many relation i.e one project can contain N phases. How do I mention foreign key as projectID in Phase class and i don't want to use ...

12. OpenJPA Foreign Key

Hi all, I have two entity classes : Customers and Users with primary keys customer_id and user_id. I need to set up a foreign key in Users named customer_id. What is the syntax of declaring a foreign key? And how to set up a relation One-to-Many between this tables? @Entity(name = "CUSTOMERS") public class Customer { @Id @Column(name = "CUSTOMER_ID", nullable ...

13. Hibernate 2 Foreign keys

14. [Solved] The foreign key is not being displayed!

Author Message Yeamin Post subject: [Solved] The foreign key is not being displayed! Posted: Thu Dec 17, 2009 9:08 pm Newbie Joined: Mon Dec 07, 2009 5:22 pm Posts: 2 Hi. I am a newbie in Hibernate and I have been trying to develop a simple Hibernate project for my last year project at university. This is my first ...

15. How to create Many to One assocation where no foreign key

Auditlog long auditlog_id PK string businessname long businessid ... Cancellation long cancellation_id PK ... I have above two Tables Auditlog and Cancellation When Auditlog's businessname is "Cancellation", then Auditlog businessid is Cancellation's cancellation_id If I could use HQL to describe the relation between Cancellation and Auditlog , it looks like below select cancel from Cancellation cancel, Auditlog audit where audit.businessname="CANCELLATION" and ...

16. reengineering tools creating foreign keys

Hi, I've seen this topic discussed in a previous thread - but the same conditions don't appear to apply to my problem. I am using mysql 5 and hibernate tools 3.24 I have the following table defined CREATE TABLE `category` ( `CategoryID` bigint(20) NOT NULL, `Category` varchar(120) DEFAULT NULL, `SectionID` bigint(20) DEFAULT NULL, PRIMARY KEY (`CategoryID`), KEY `SectionID` (`SectionID`), CONSTRAINT `category_ibfk_1` ...

17. Foreign key referencing nonprimary keys

I have a scenario where the foreignkey in the child table references a part of primarykey in the parent table which is also unique. This is an existing database. I remapped the unique primary key part and used the property-ref attribute to reference the remapped property. I am getting the following error. 29-Mar-2010 11:24: AM. ERROR BasicPropertyAccessor.get:191 - IllegalArgumentException in class: ...

18. Foreign key in hibernate

Hi All, I am having two tables 1)project 2)Phase Project table contains: projectId projectName startDate endDate Phase table contains : projectId as foregin key phaseId as primary key phaseName startDate endDate this is one to many relation i.e one project can contain N phases. How do i mention foergin as projectID in Phase class? Code:

19. Self Referencing Foreign Key problem

Author Message raystorm Post subject: Self Referencing Foreign Key problem Posted: Mon Jun 07, 2010 4:43 pm Newbie Joined: Wed May 12, 2010 8:29 pm Posts: 4 I have an Entity Employee, that maps to 2 database tables using a join. Employee has a property supervisorPcn that maps to pcn. Both of these properties map to the joined table ...

20. hibernate and foreign key

21. Foreign Keys, Set Problem

Beginner Joined: Thu Jun 17, 2010 7:36 am Posts: 26 Hello, Ive got a problem. I know here is a lot of code but it looks worse than it is :-) Im working on a small webapplication with hibernate and spring. Ive got (to explain I reduced it a little bit) two tables: users(username, password, enabled) --> pk username userinformation(email,username,etc) --> ...

22. Problem with @ANY and Foreign-Key

@Entity @Table(name = "USR_USER") public class User extends PersistentObject { [...] @Any(metaColumn = @Column(name = "USR_OWNERTYPE")) @AnyMetaDef(idType = "long", metaType = "string", metaValues = { @MetaValue(targetEntity = Customer.class, value = "CST"), @MetaValue(targetEntity = Client.class, ...

23. question about foreign key

24. foreign-key not set using many-to-one

25. One-to-One with "Reverse" Foreign Key?

I've got a one-to-one relationship, like a Customer and its Address: I'd like the object graph to be navigable only from Customer to its Address (the other way makes no sense). Unfortunately, the existing database model (which cannot be changed) implements the relationship as a "fake" foreign key from the Address to the primary key of the Customer. Fake, because there ...

26. Foreign key constrain

Sorry, the above SQL 's variable does not match the xml provided. The item should be purchaseItem and POrder should be purchaseOrder. They are extracted from part of the code and I have rename some of the varible to make it better understanding. Anyway, am I got something wrong in the hbm2ddl ?

27. cannot produce foreign key

public class PCard{ private String id; private String serial; ..... } public class PSession { private String id; private Date startTime; private Date endTime; private PCard card; ...... } ...

28. many to many foreign keys

If I have tableA with id "a_ID" and tableB with id "b_ID" and property "b_PROP" and many to many lookup table with properties "a_ID" and "b_PROP" is there any way I can map this many to many relationship with Hibernate? My understanding is the collection key would have to be "a_ID". The column property in the many to many association would ...

29. one-to-many, set keyword diff between foreign-key & colu

Sorry about this stupid question ... When specifying a one-to-many relation I wonder how I can reference a column name in the parent to a coulmn name in the child which do not have the same name. (NAME is the column name in the Child Table which links to the column name PARENT_NAME in the Parent) In my parent mapping I ...

30. DTD Element for "key" I see "key foreign-key&

31. Foreign key issue

I have these 4 tables and 4 java classes. 2,3,4 tables are represented as 'Set' methods in WeatherLocation class. My problem is my company's db schema is not normalized properly and it does not have referential integrity, no FK. Looking at the previous sql select statements i implemented the following mapping file. Now i am getting the following error. Is this ...

32. multiple foreign keys

33. Foreign Keys

Hi all, I'm using MySQL 4.0.15 with Hibernate 2.1.2 and have a couple questions regarding Hibenate's SchemaUpdate. 1. Why does SchemaUpdate continually try to create the foreign key contraints on the MySQL database tables until it maxs them out to having 32 keys each? 2. Is there a setting to control this that I've missed in the documentation? 3. Why is ...

34. Problems adding in foreign Keys

I have code that creates a CSVFile object, adds 3 records to it and save it, then ad another CSVFile and add two records and save it, but when I call the commit() methot on the transaction I get the following stacktrace net.sf.hibernate.JDBCException: could not insert collection: [] at net.sf.hibernate.collection.AbstractCollectionPersister.recreate( at net.sf.hibernate.impl.ScheduledCollectionRecreate.execute( at net.sf.hibernate.impl.SessionImpl.executeAll( at net.sf.hibernate.impl.SessionImpl.execute( at net.sf.hibernate.impl.SessionImpl.flush( at net.sf.hibernate.transaction.JDBCTransaction.commit( at ...

36. Populating foreign key

I have an Organisation class, which contains a List of OrganisationAddress objects. I am finding that when I try to save an address by adding it to the list and saving the parent object as in the code below, unless I have the oa.setOrganisation line the address objects get saved without the foreign keys linking them to the organisation table. I ...

37. foreign key references for assigned keys

... ...

38. Type conversion for a foreign key

Hibernate version: 3.0beta4 Hi, i'd like use hibernate on an exsiting schema (that i can't modify). I have several tables (be A and B) and a special table containing some labels. The primary key of A is a long. The primary key of B is a string. So the description of my labels table is: foreign_key: string type: string ('A' or ...

39. Mulitple Foreign Keys

Hi All: If I have TABLE_A, TABLE_B and TABLE_C and TABLE_C has a foreign key that is associated with TABLE_A and another foreign key associated with TABLE_B how can I show the relationship in the hibernate.xml file. In other words, TABLE_C is a child of both TABLE_A and TABLE_B. Can I use the many-to-many relationship? Thank you in advance, Om

40. General Foreign Key DAOTest Problem

Beginner Joined: Fri May 21, 2004 3:49 pm Posts: 21 Location: Germany, Mainz Hi, I have got a big problem, I want to use those two mapping files and create a DAOTest for it. I added the DAOTest below. When I run this test I get the getter Exception, allthough they exist. The mapping files and the java classes, both were ...

41. one-to-one foreign key assosciation

I have a one-to-one mapping based on a foreign key association. It is able to pull the related data. The problem is: when saving it does not put in an id into the foreign key of the related data . tre.QUERY_GROUP_SEQ

42. Foreign key issue

Hi all, I have two tables-1.student(student_id,student_name),,student_id,city).Here student_id is the pk of student and fk of info.I want to know that should i mention the pk and fk constrain during my sql create table statement or it is sufficient to mention the mapping tags in the hbm file.If is it in hbm file than how to write this.I have gone through ...

43. foreign key problem

Newbie Joined: Mon Sep 12, 2005 4:32 am Posts: 1 Location: Posen - Poland Hi all; I use hibernate 3.1 beta 1 and I am encountering following problem during schema generation: org.hibernate.MappingException: Foreign key (FK2E507FF63F66BF:bugs [pid])) must have same number of columns as the referenced primary key (projects [nickname,pid]) Here are my mappings: Code: ...

44. foreign key

45. Navigability and foreign key

Hi, If I have two entities, say Location and Address, with a one-to-one relation, is there any way I can make the relation uni-directional from Location to Address, if the foreign key is on the ADDRESS table and not the LOCATION table. The only way I can get it working is to have the ADDRESS_ID column in the LOCATION table. If ...

46. Foreign Key Problem

Hibernate version: 3.05 Name and version of the database you are using: IBM DB2 v. 8.2 I am fairly new to Hibernate and I am trying to convert an existing application to use Hibernate for persistence. I am having difficulty with some of the mappings, so any help is greatly appreciated. The Error I am getting is: SessionFactory creation Repeated ...

47. one-to-one foreign key problem

Hi, I have a class(A) that contains objects of classes(B,C,D). I have used a one-to-one relationship. But it is a Unidirectional relationship. I want to use foreign keys to relate the two. I have put one-to one mappings in class (A) and no back mapping in class B,C,D as its a unidirectional relationship. Now if I want a foreign key column ...

48. one-to-one foreign key problem

Hi, I have a class(A) that contains objects of classes(B,C,D). I have used a one-to-one relationship. But it is a Unidirectional relationship. I want to use foreign keys to relate the two. I have put one-to one mappings in class (A) and no back mapping in class B,C,D as its a unidirectional relationship. Now if I want a foreign key column ...

49. DOM4J mode - problem setting foreign keys of nested elements

Hibernate version: 3.1rc3 Name and version of the database you are using: Oracle 10g Mapping documents, source code etc.: Zipped & available on req. Hello Hibernate Community, I am trying to use hibernates xml/relational persistence but I'm having problems because I can't setup the foreign keys of my child elements properly. The XML I want to persist looks like this...

50. How to input foreign key?

Beginner Joined: Mon Sep 19, 2005 3:59 pm Posts: 31 Hi there, I am trying to modify Laliluna's sample of Struts and Hibernate to insert customer when typing in Book information. In bookAdd.jsp, I can get all customers in drop down box, after I type in all fields and select one customer, and submit, it says Quote: argument type mismatch . ...

51. Double foreign key

Hello all, has anyone ever faced the problem "HibernateSystemException: a different object with the same identifier value was already associated with the session"? We have a double relation Claim - Event and Item - Event, and also a one-to-many relationship between Claim and Item. So in this case, an event belongs to a claim while the same event may also belong ...

52. Just cannot figure this foreign key problem out.

I've been trying to figure this out for hours now. Everything is there and it seems like it should work, but for some reason it is just not. Can someone please help me? I'll keep this as brief as possible. Here is my mapping: Code:

53. Problem refering foreign key directly...

54. Using a filter as a foreign key in a set?

56. many-to-many not setting foreign keys

57. Many to one... dealing only with the foreign key...

I have a many to one mapping and i want only to deal with the foreign key of the mapped object instead of retrieving the whole mapped object... Long id = myobjekt.getMappedObject().getId(); will retrieve the whole (large) MappedObjekt, and get the id, but i only want to deal with the id... Is there a way to get the foreign key value ...

58. [one-to-one] with blank in the foreign key

hello i have a relation one-to-one with foreign key like this Code:

59. Dom4J Foreign Key access

Hello All, We have 2 tables a POINTER_CACHE and PERSON_ACTIVITY_HEADERS table. We are using the Hibernate Dom4J capabilities and for the most part they are working great. The Header table is associated with the Cache table using a foreign key. In my code, however, I am unable to extract this foreign key from the DOM4J node that is returned that represents ...

60. one-to-many mappding ignoring foreign key

I have posted this question in the NHibernate forum a few days ago, but since it's not .Net specific (in my opinioin) I'm posting it here again, hoping I will get more feedback. Here's what I have: a ResourceString table with an Id (not very meaningful) and a ResourceId (Guid) which forms a secundary key with the field IsoLangCode. Most entity ...

61. Telling hibernate not to enforce foreign keys.


63. multicolumn foreign key

Hi, I have 2 columns (conditionGroupId,primeResultId) in 1 table(Totsodrs) which are foreign keys related to another table(Rsltcondval). How can I specify this in my hibernate mapping file please? I got this error: Duplicate property mapping of rsltcondval found in com.galderma.rkp.researchproduct.beans.Totsodrs Here is my Hibernate mapping file:

64. Associatons and missing foreign keys

I am implementing an application with Hibernate running towards an existing Oracle database. This database has no "physical" foreign key constraints defined, even if they logically exists. Will I still be able to use the various associations in Hibernate (one-to-many etc), even if I there are no actual foreign keys defined in the database?

65. Foreign key Prob

Hi everyone, I'm a newbie to Hibernate and i'm just having issues with linking two tables using a bi-directional one-to-many association. Background info: Using hibernate on an existing db which is required to map data and objects across multiple schemas. First table, LA_Licenses has a varchar primary key in the format '10D136012' and exists on schema LA. It has a foreign ...

66. Efficient way of setting foreign keys without lookups?

I'm currently porting an application from JDBC to Hibernate and have some concerns about the performance overhead and would like to ask more experienced Hibernate users what the best solution is for my problem. Rather than explaining the application in detail, I've summarised the general issue I have concerns with. Imagine a typical web site selling products ... The customer is ...

67. many-to-one not created as foreign key

I have a case where the sql schema fails to recognize and declare a foreignkey. I am using mysql and hibernate 3.2 parent: hi_value next_value 100 child:

68. findByExample foreign-key problem

Hi, i run into the following problem with hibernate 3.2 : i have an set of employees belonging to an department and try to get one of the employees by findByExample() ... ... ... hibnernate findByExample() will include a foreign-key column 'deptId' into the generated sql-query, ...

69. Foreign key problem

70. Impossible to set reference to foreign key

Hello, i have a problem with code generate by Reverse engineering. I have the following table: ASSISTIBILI, which has Primary Key : PKASSISTIBILI Foreign Key: ASSISTIBILI_FK which refers to ASSISTIBILI.PKASSISTIBILI This is a composite, where each record can have a reference to another record in the same table. the association is *--->1 The generated code contains the following methods: public Assistibili ...

71. foreign key referencing nonprimary key issue with composites

Newbie Joined: Thu Jan 31, 2008 7:45 am Posts: 1 I've been trying to map part of a legacy database and have run across a problem when using a one-to-many mapping with a foreign key referencing a non-primary key. All is fine as long as the class containing the collection uses a single primary key. When the class uses a composite ...

72. [Solved]Bi-directional one-to-one & Foreign key

public class User { @Id @Column(name = "user_id") @GeneratedValue(strategy = GenerationType.AUTO) public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } private Integer userId; @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @PrimaryKeyJoinColumn public Demographics getDemographics() { return ...

73. Nonprimary foreign key

Hi All, My requirement is to have 1 unique Non Primary key act as a Foreign key : Table A Table B Field1 PK Field1 PK Field 2 UQ---------------->Field2 Field3 Field3 Any Idea Does Hibernate support this kind of relationship ? Since while adding data into TableB Its taking Field1 of TableA instead of taking Field2. Many Thanks in advance!!! Best ...

74. Problems with reflexive foreign key


75. foreign keys setup question

I am somewhat stuck adding a FK constraint via hibernate and thus seeking help/feedback. At first the problem seemed simple: hibenate mappings listed below; the tables will be populated with data from a script, so needed FK to ensure data integrity. I planned to use hibernate only to read data into beans. The fields in the Java classes match columns in ...

76. One-to-many without foreign key. Is this possible?

Hibernate version: 3.2.6 Mapping documents: Blueprint Mapping Document Score:

77. How can I use many-to-one without foreign-key

78. ***Foreign key reference problem***

Hi, ...

79. Hibnerate and foreign keys

80. re-creation of foreign keys

Newbie Joined: Tue Dec 16, 2008 6:59 am Posts: 6 Hi everyone thanks for reading this post. I observed a tricky point about hibernate and my database. Basicly, hibernate seems to re-create the foreign keys I previously defined in the database. Hibernate version: 3.2.4 Mapping documents: Code: package org.cnio.appform.entity; import static javax.persistence.GenerationType.SEQUENCE; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import ...

81. is it possible to edit foreign key in Hibernate?

82. Scalar foreign keys?

Hello, I am using JPA\Hibernate and would like to know if it is possible to create a foreign key contraint on a scalar value? For example, I have a Person entity and an Address entity. Instead of having a relationship and putting Address as a member of Person, I would like Person to have an integer addressId. To preserve referencial integrity, ...

83. Disable creation of foreign keys

84. Foreign key used in set of sublass (hibernate 3.2 -> 3.3.)

Hi, I used the following mapping in Hibernate 3.2 successfully. However, in Hibernate 3.3.1 GA I receive a "property not found" error: (org.hibernate.MappingException: property [address] not found on entity []) Do I have to use a different mapping now? Or should HbmBinder.bindCollectionSecondPass use collection.getOwner().getReferencedProperty(property).getValue() instead of getRecursiveProperty in line 2429? I have a class Server, a subclass ServerImpl and a referenced ...

85. Hibenrate is adding Foreign Keys

Hi everybody, I am using Hibernate synchronizer to do my final studies' project, but i'am facing a problem I explain : I have a database composed of tables having (inheritance and composition) in some cases.. I've done the .hbm.xml mapping correctly.. but when i make a query on those tables from my java code (Eclipse), I realise that new Foreign Keys ...

86. Doubt with foreign keys

Hi, I have two foreign keys which I don't know how to map. Can anybody help me? 1) FOREIGN KEY (postalcode) REFERENCES city_postalcode (id) ON DELETE CASCADE @ManyToOne(cascade=CascadeType.REMOVE,optional=false,fetch=FetchType.LAZY) @Cascade({org.hibernate.annotations.CascadeType.REMOVE, org.hibernate.annotations.CascadeType.DELETE_ORPHAN}) @JoinColumn(name="country", nullable=false, updatable=true) public Country getCountry() { return country; } 2) FOREIGN KEY (createdby) REFERENCES uservm(id) ON DELETE SET NULL @ManyToOne(optional=true,fetch=FetchType.LAZY) @JoinColumn(name="createdby", nullable=true, updatable=true) public UserVM getCreatedBy() { return createdBy; } ...

87. How to change reference type of foreign key.

Hi, can anybody give me idea how to change the foreign key into something like bigint() type or just save the id of parentId instead of object itself. I just started using Hibernate 3 and annotation. And it's giving me a default type of tinyblob and saving the object literally which is pain in file size. Here is what im doing: ...

88. hibernate foreign key  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.