Hibernate : Netbeans genenerates additional POJO class - java

I got 3 tables :
Actor : ID, Name
Movie : ID, Title
Cast : Movie_ID, Actor_ID
Now I want to JOIN those 3 tables to get actor's list for each movie. It is simple SQL query, but it gets harder with Hibernate.
First of all, I created Cast table like this :
CREATE TABLE Cast (
Movie_id int PRIMARY KEY,
Actor_id int PRIMARY KEY,
FOREIGN KEY (Movie_id) REFERENCES Movie(ID),
FOREIGN KEY (Actor_id) REFERENCES Actor(ID)
)
Then, in Netbeans, I chose New -> Hibernate Mapping Files and POJO's from Database, I added ACTOR, MOVIE and CAST tables and IDE generated 4 instead of 3 classes : Actor.java, Movie.java, Cast.java and CastId.java. Class CastId.java seems to be well mapping of Cast table and Cast.java includes only CastId field and getters and setters.
I understand that this is the way Netbeans handles tables which keys have multiple attributes - but why , how does it help me ? Let's say that I want to SELECT * from Cast table which should return list of pairs(Movie_ID, Actor_ID). I can't make it, beacuase when running HQL command
from Cast
it gets only some single ID's - it's beacase Cast has only one field : CastId. On the other hand Netbeans generates CastId but doesn't CastId.hbm.xml, so I can't run 'from CastId' either. Am I missing something ? I'm newbie at Hibernate and would like to understand how this should be done.

Related

How to design database SQLITE if the model include another list model

My application need to store Model_A into SQLITE database, and Model_A include a ArrayList of Model_B as I describe in example code below:
class Model_A {
String name;
String description;
int number;
List<Model_B> list_Model_B;
}
class Model_B {
String title;
int quantity;
}
I searched and know that I can use GSON (or JSONObject) to convert Model_B to a json String file and store this json in Table of Model_A. But this way is not good for handling the data. I just want to do it without using GSON.
I thought about create two table for Model_A and Model_B. Table of Model_A will store a column ID of Model_B. But this way only can store a object Model_B, not a List as I expect.
Can you let me know how to design the database reach my expect ? Or please suggest me a library which can help me on this case. Thanks.
According to relation database theory, you need to build one-to-many relationship, using two tables - the first one for Model_A, the second - for Model_B.
Main idea of this relationship is connect two entities with foreign keys to have possibility for querying.
Let's write some SQL for creating such relationship:
CREATE TABLE model_a (
_id INTEGER PRIMARY KEY,
field TEXT NOT NULL
);
-- Table for Model_B
CREATE TABLE model_b (
_id INTEGER PRIMARY KEY,
model_a_id INTEGER NOT NULL,
field_2 TEXT NOT NULL,
FOREIGN KEY model_a_id REFERENCES model_a(_id)
);
OK, we created two tables. But how can we get List<Model_B> from such scheme?
There are two ways to do it.
The first - make two selects for every Model_A class object: one for "details" of this object from model_a table, and one for list of Model_B elements:
SELECT *
FROM model_a
WHERE _id = ?
-- For list of Model_B
SELECT *
FROM model_b
WHERE model_a_id = ?
The second way - get all information about one Model_A by one query. For this task we need special SELECT query:
SELECT *
FROM model_a ma
INNER JOIN model_b mb ON (ma._id = mb.model_a_id)
WHERE (mb.model_a_id = ?)
Hope it helps.

Organize database (how to store a list)

In my database I have table "Announcement" with fields:
id int,
text varchar,
sent_date date.
Also table "Group" with fields:
id int,
name varchar.
Each announcement submitted to any number of groups(from 1 to all). How to store this relation in database?
I'm using MySQL and Hibernate in java web project.
Create a third table (I call it announcement_group) with two columns:
announcement_id REFERENCES Announcement(id)
group_id REFERENCES Group (id)
Read more about Foreign Keys from the documentation here http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
In your query you will want to use a
SELECT group_id WHERE announcement_id=<your id here> from announcement_group;

I can not generate Hibernate Mapping Files and POJOs from Database PostgreSQL?

have created my tables and relationships in the database PostgreSQL, but when I want to generate Hibernate Mapping Files and POJOs, they are not generated
I applied all the appropriate steps to hibernate.cfg.xml generation and hibernate.reveng.xml
I think it's because the name tables and fields that I have in all uppercase, because I tested with another BD with the names of the tables in lower case and if it works normally, I show the script of my tables.
CREATE TABLE "public"."T_LNEA"(
"ID_LNEA" Integer NOT NULL,
"ID_CTGRIA" Integer NOT NULL,
"DSCRPCION" Character varying(200)
)
WITH (OIDS=FALSE)
;
ALTER TABLE "public"."T_LNEA" ADD CONSTRAINT "PK_ID_LNEA" PRIMARY KEY ("ID_LNEA")
;
CREATE TABLE "public"."T_SUB_LNEA"(
"ID_SUB_LNEA" Integer NOT NULL,
"ID_LNEA" Integer NOT NULL,
"DSCRPCION" Character varying(200)
)
WITH (OIDS=FALSE)
;
-- Add keys for table public.T_SUB_LNEA
ALTER TABLE "public"."T_SUB_LNEA" ADD CONSTRAINT "PK_ID_SUB_LNEA" PRIMARY KEY ("ID_SUB_LNEA")
;
CREATE TABLE "public"."T_CTGRIA"(
"ID_CTGRIA" Integer NOT NULL,
"DSCRPCION" Character varying(200)
)
WITH (OIDS=FALSE)
;
ALTER TABLE "public"."T_CTGRIA" ADD CONSTRAINT "PK_ID_CRITERIA" PRIMARY KEY ("ID_CTGRIA")
;
And an image that is loading the tables using the JBOS Tools.
But still I need support because I can not generate the POJOs.
In the File "hibernate.reveng.xml" has the following
<schema-selection match-catalog="mybd" match-schema="mybd"/>
You have to delete the match-schema="mybd".
By default, when you use postgresql in hibernate, appears the match- schema , which does not happen in mysql , delete it and work , checked in Netbeans8.0
Hi, as said before, if I change the name of the table and its fields in lowercase, if generates POJOs, the question is why is not generated in capital letters .....
Une the reverse engineering tools provided by Hibernate. It depends on your IDE.
For example with NetBeans this is how it is done: https://netbeans.org/kb/docs/web/hibernate-webapp.html
You solve it somehow does not recognize the table name in uppercase, but if you recognize the attributes of the table in uppercase, so rename the tables in lowercase temporarily after he change in the EJB annotations, at least it works somehow.

Delete and insert with same primary key in place of update

I have a simple One-to-One relationship between table TICKETINFO and TICKETINFO_REMARK.
TICKETINFO
TICKETINFOID pk, REMARK varchar(128), TICKETDATE timestamp
and
TICKETINFO_REMARK
TICKETINFOID fk, REMARK varchar(128)
and TICKETINFOID will be foreign key from TICKETINFO table and have to populate the REMARK field of TICKETINFO_REMARK along with the REMARK field of TICKETINFO for the corresponding TICKETINFOID.
For 1 TICKETINFOID there will be one REMARK.
The insertion is working fine.
Now my problem is:
My requirement is that I am not supposed to update any record in TICKETINFO_REMARK due to some performance issue. I need to delete the record first and insert it with the same TICKETINFOID and the new REMARK.
for example:
TICKETINFO table has:
**TICKETINFOID** : 1
**REMARK** : ABC
**TICKETDATE** :2012-06-12
TICKETINFO_REMARK has:
**TICKETINFOID** : 1
**REMARK** : ABC
Now I want to change the REMARK in TICKETINFO_REMARK to "XYZ123"
So i have to delete the entry from TICKETINFO_REMARK and reinsert it which which look like this
**TICKETINFOID** : 1
**REMARK** : XYZ123
what will be the code to do it?
please help!!!!
First you should select a record form "TICKETINFO_REMARK" table with TICKETINFOID.If it already found you should delete this record from "TICKETINFO_REMARK" table and then insert new record with this TICKETINFOID.It is only logic for your requirement.If you want to get sample code, please tell me which technology did you use for your CRUD operation for example JPA, HIBERNATE, IBATIS OR JDBC etc.

JPA ids auto_increment generation

Am using JPA into my application and then the when insert different objects the database use sequential ids.
For Example:
If i have table member and table user , when inserting object of type user will take id = 1 then add object into table member will take id = 2 .
The problem is : i imported a .sql file into the database.
Then when i insert a new record from my application it cause an exception because it use ids already added.
How can i solve this problem ?
You can set the start value for AUTO_INCREMENT as
ALTER TABLE tbl AUTO_INCREMENT = 100;
See also:
3.6.9. Using AUTO_INCREMENT

Categories

Resources