NetBeans Forums
| View previous topic :: View next topic |
| Author |
Message |
Andres_Meemo
Joined: 28 Mar 2011 Posts: 2
|
Posted: Mon Mar 28, 2011 6:03 am Post subject: Entity classes from Database: Detect Relation Direction from Schema. |
|
|
Hello Everyone,
I am trying to generate "Entity classes from Database" using the wizard (Netbeans 6.9.1). But it seems it cannot recognize relation directions, e.g:
Scenario:
Table Exam has a unique field ExamName
Table Question has a field TheQuestion
Table (View) ExamQuestion that represents the many-to-many relationship between Exam and Question (one question may be present on one or many exams, and an Exam can have one or more Questions)
When generating Entities from database, the wizard creates a List of Exams for Question, which is good... BUT a List of Questions for each Exam (joining the ExamQuestion View), though the Exam shouldn’t know anything about the Questions assigned to it. This is a BIG problem if you have 160.000 registers on Question table and a few Exams.
It happens no matter which provider I should use, e.g. Hibernate, Eclipselink, etc.
Is it possible to assure relation direction for entities generated from database?
Thanks in advance!
AM |
|
| Back to top |
|
 |
Tony Posted via mailing list.
|
Posted: Tue Mar 29, 2011 12:13 am Post subject: Re: Detect Unidirectional Relations from Schema (Entities from DB) |
|
|
What are you using as your foreign keys? And where are you specifying them? Which direction do they go? If you specify both relationships in the View ExamQuestion, then you get the result you have currently. Do you need the relationship between the Exam table specified in the ExamQuestion table or in the Exam table?
If you specify the relationships as
Question ->ExamQuestion -> Exam,
then the Question entity will know about the related ExamQuestion(s), the ExamQuestions will know about the related Exams, but the Exams won't know anything about the Questions.
i.e. Questions has a foreign key on ExamQuestions and ExamQuestions has a foreign key on Exams. (I think I've got that right, but I'm working this out in my head at 1am, so forgive me if I have it wrong .. the general idea is right! /grin)
In any case, even if you keep your currently generated entities, there's a simple solution .. you don't HAVE to keep the relationship collection you don't want .. just delete the unwanted relationship from the generated class.
Or keep it and never use it, as the performance penalty isn't that high, since the collection is lazily instantiated .. i.e. it is only fully populated if you actually call it.
Tony
-----Original Message-----
From: Andres_Meemo [mailto:address-removed]
Sent: 28 March 2011 07:04
To: address-removed
Subject: [nbj2ee] Detect Unidirectional Relations from Schema (Entities from DB)
Hello Everyone,
I am trying to generate "Entity classes from Database" using the wizard (Netbeans 6.9.1). But it seems it cannot recognize relation directions, e.g:
Scenario:
Table Exam has a unique field ExamName
Table Question has a field TheQuestion
Table (View) ExamQuestion that represents the many-to-many relationship between Exam and Question (one question may be present on one or many exams, and an Exam can have one or more Questions)
When generating Entities from database, the wizard creates a List of Exams for ExamQuestion, which is good... BUT a List of ExamQuestion for each Exam, though the Exam shouldn’t know anything about ExamQuestion. This is a BIG problem if you have 160.000 registers on Question table and a few Exams.
It happens no matter which provider I should use, e.g. Hibernate, Eclipselink, etc.
Is it possible to assure relation direction for entities generated from database?
Thanks in advance!
AM |
|
| Back to top |
|
 |
Andres_Meemo
Joined: 28 Mar 2011 Posts: 2
|
Posted: Tue Mar 29, 2011 1:43 am Post subject: Re: Detect Unidirectional Relations from Schema (Entities from DB) |
|
|
Thankyou Tony,
You got it right!!!
I would like to:
Question knows what exam it belongs to,
Exam won’t know anything about the questions,
As it exist a many-to-many relationship between Question and Exam, ExamQuestion view is needed, so:
ExamQuestion should have a Foreign Key to Question, and a Foreign Key to Exam.
When generating Entities, there is no ExamQuestion entity generated, but a Question entity with a List of Exams, and an Exam entity with a list of Questions (which I wouldn´t like), and a JoinTable annotation to ExamQuestion view (on Exam entity): the pure many-to-many relationship.
| Code: | @Entity
@Table(name = "question")
public class Question implements Serializable {
...
@ManyToMany(mappedBy = "questionList", fetch = FetchType.LAZY)
private List<Exam> examList;
...
}
@Entity
@Table(name = "exam")
public class Exam implements Serializable {
...
@JoinTable(name = "examquestion", joinColumns = {...}
@ManyToMany(fetch = FetchType.LAZY)
private List<question> questionList;
...
}
|
You are right about deleting the redundant Question List on Exam, but the Exam list on Question is "attached" to it... hence, a runtime exception when deleting it: "the attribute List<Exam> on entity Question points to an attribute on Exam which doesn't exists" (in other words). So, deleting is not an option.
It is a good tip, the one about lazy instantiation… I didn´t know. But these objects are sent over the network to a flash client using BlazeDS. The application works perfect when using a DB with a few registers… but it crashes when using the entire Data. So, I think these attributes (Lists) are overloading the system.
Here is the code for the DB; please let me know if you find what would be wrong on Foreign Keys declaration.
| Code: | # Dumping structure for table sometable.question
DROP TABLE IF EXISTS `question`;
CREATE TABLE IF NOT EXISTS `question` (
`Name_pk` varchar(255) NOT NULL,
PRIMARY KEY (`Name_pk`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
# Dumping structure for table sometable.exam
DROP TABLE IF EXISTS `exam`;
CREATE TABLE IF NOT EXISTS `exam` (
`Name_pk` varchar(255) NOT NULL,
`Desccription` varchar(255) DEFAULT NULL,
PRIMARY KEY (`Name_pk`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
# Dumping structure for table sometable.examquestion
DROP TABLE IF EXISTS `examquestion`;
CREATE TABLE IF NOT EXISTS `examquestion` (
`questionName_pk` varchar(255) NOT NULL,
`ExamName_pk` varchar(255) NOT NULL,
PRIMARY KEY (`questionName_pk`,`ExamName_pk`),
KEY `IncludesInProgram` (`ExamName_pk`),
KEY `BelongsTo` (`questionName_pk`),
CONSTRAINT `BelongsTo` FOREIGN KEY (`questionName_pk`) REFERENCES `question` (`Name_pk`),
CONSTRAINT `IncludesInProgram` FOREIGN KEY (`ExamName_pk`) REFERENCES `exam` (`Name_pk`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
Thanks in advance,
AM |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|