JSF datatable columns from multiple database tables - spring

I have a JSF datatable, it has three columns, those are:Work_Type_Desc, Project_Phase, and Activity_Desc. Those columns comes from 2 different database tables, the relationship of those two tables are one-to-many.
The 1st Table name is Work_Type. It has 1) Work_Type_Cd, 2)Work_Type_Desc, 3)Created_By_Name, 4)Created_DT, 5)Updated_By_Name, 6) Updated_DT
The 2nd Table name is Activity_Type. It has 1)Activity_Cd,2) Work_Type_Cd,3)Project_Phase,4)Activity_Desc, 5)Created_By_Name, 6)Created_DT, 7)Updated_By_Name, 8) Updated_DT.
I use Hibernate+Spring+JSF, my question is how to show those three column records in JSF datatable, do I need to create a new model domain class to store this two tables properties? If so, how to handle the PK and FK in the new model class. Thanks!

Just let your service layer return a List<ActivityType>. The WorkType is already referenced by #ManyToOne property in ActivityType, right?
<h:dataTable value="#{bean.activityTypes}" var="activityType">
<h:column>#{activityType.workType.workTypeDesc}</h:column>
<h:column>#{activityType.projectPhase}</h:column>
<h:column>#{activityType.activityDesc}</h:column>
</h:dataTable>
It's generally unnecessary to create another mapping layer for that.

Related

Spring jdbc repository many-to-many conjunction table column names

I can't find where to define custom column name for conjunction table in case of many-to-many relationship in spring-data-jdbc.
I extended aggregate example from https://github.com/spring-projects/spring-data-examples in my fork: https://github.com/konstiak/spring-data-examples/commit/2a901bb4d81c35406da393b1368109136ae21f5f.
If the conjuction table has columns [color, lego_set] it works out of box. But I'd like to have custom names for these columns [color_id, lego_set_id]. It's clear for 'color_id'. I can define it by #Column annotation in ColorRef entity. But how can I define that column for LegoSet.id will be stored in 'lego_set_id'?
RESOLVED
I just had to define #Column(value = "lego_set_id") on colors field in LegoSet entity.
Defining #Column(value = "lego_set_id") on colors field in LegoSet entity resolved the problem.

Get back data from the pivot table when store a many-to-many relationship

I have a many-to-many relationship between projects and surveys. I can successfully create a relationship between a survey and a project with
$userSurvey = $project->surveys()->save($survey);.
This will create a new record inside the question_survey pivot table (The pivot table contains the columns id, question_id and survey_id.)
$userSurvey will receive the newly created survey model. Is there any way to receive also the id of the new record in the question_survey pivot table?
When retrieving many to many relationships, Laravel will automatically attach the pivot to the resulting Model, so in your case, $userSurvey will automatically have an attribute called pivot that holds, well, of course, the pivot.
But by default, that pivot attribute only holds the foreign keys, so in your case, the question_id and survey_id. If you have any other extra attributes,(in your case id), simply use the withPivot method, as follows.
public function surveys()
{
return $this->belongsToMany('App\Question', 'question_survey')
->withPivot('id');
}
Now you can access the id from the pivot table:
$userSurvey->pivot->id;
Bonus, if you think that the pivot word just does not fit your wording style, just use the as method in your relationship to customize the variable name of the pivot attribute.
public function surveys()
{
return $this->belongsToMany('App\Question', 'question_survey')
->as('question_survey')
->withPivot('id');
}
Now you can access the id from the pivot table:
$userSurvey->question_survey->id;

VFP 9.0 how to extract all the table relations from a DBC object

I need to extract all the relationships between Parent/Child tables somehow. Is there a way to do that using the DBC object?
Sorry guys I forgot it over the years, but after some thinking it came to me...
OPEN DATABASE ABC
=ADBOBJECTS(ga_Connections, "CONNECTION")
=ADBOBJECTS(ga_Relations, "RELATION")
=ADBOBJECTS(ga_Views, "VIEW")
=ADBOBJECTS(ga_Tables, "TABLE")
CLOSE DATABASES
The global array ga_Relations is an N x 5 array with
Parent table, Child table, Parent key, Child key, empty string cell
You can create a cursor and insert the array into it as follows:
CREATE CURSOR dbRelat (Parent_Table C(40),Child_Table C(40),Parent_Key C(40),Child_key C(40),Spacer C(1))
TRY
INSERT INTO dbRelat FROM ARRAY ga_Relations
CATCH
MESSAGEBOX("No Relations in this DataBase!",48,1000)
ENDTRY
And of course you can do the same for the TABLES, VIEWS and CONNECTIONS...
Hope it is useful to others...
DK

Retrieving rows based on a certain criteria regarding a many-to-many mapping in Hibernate

I'm just copy & pasting some of the introductory text from one of my questions, since the same table relationship is involved in this question also.
I have three of many tables in Oracle (10g) database as listed below. I'm using Hibernate Tools 3.2.1.GA with Spring version 3.0.2.
Product - parent table
Colour - parent table
ProductColour - join table - references colourId and prodId of Colour and Product tables respectively
Where the table ProductColour is a join table between Product and Colour. As the table names imply, there is a many-to-many relationship between Product and Colour which is mapped by PrductColour. I think, the relationship in the database can easily be imagined and is clear with only this much information. Therefore, I'm not going to explore this relationship at length unnecessarily.
An entity (row) in Product is associated with any number entities in Colour and an entity (row) in Colour can also be associated with any number of entities in Product.
Since, it is a many-to-many relationship, it is mapped in the Product and the Colour entity classes (POJOs) with their respective java.util.Set and no direct POJO class for the product_colour table is available.
The class Product looks like the following.
public class Product implements java.io.Serializable
{
private BigDecimal prodId;
private Set<Colour> colours = new HashSet<Colour>(0);
.
.
.
//Other properties with setters and getters.
}
The class Colour looks like the following.
public class Colour implements java.io.Serializable
{
private BigDecimal colourId;
private Set<Product> products = new HashSet<Product>(0);
.
.
.
//Other properties with setters and getters.
}
The actual mapping between entities is available in xxx.hbm.xml files, regarding this question which is unnecessary, I think .
What I want to do is to retrieve only those rows from the Colour table which don't match the colour rows in the ProductColour table for a particular product at a time. In this regard, the native Oracle SQL statement would look something like the following.
SELECT colour_id, colour_name, colour_hex
FROM colour
WHERE colour_id not in (SELECT colour_id FROM product_colour WHERE prod_id=81)
ORDER BY colour_id DESC
Where prod_id can be any valid BigDecimal number in Java which is dynamic.
As noted earlier, the relationship is available as a many-to-many relationship in Hibernate, no POJO class for the database table product_colour is available and therefore, I'm fumbling in writing such an HQL statement in Hibernate. I have tried to write such an HQL statement but no attempts were succeeded.
[The code presented in the rest of the part may completely be unnecessary to review]
I'm therefore following a traditional way. What I'm doing is... I'm first retrieving a single product row from the Product the entity class based on a dynamic value of prodId such as,
List<Product>list=session.createQuery("from Product where prodId=:prodId")
.setParameter("prodId", prodId).list();
and then using a loop, I'm getting the entire Colour set - java.util.Set corresponding to the product_colour table in Oracle which is available in the Product entity for this product such as,
Set<Colour>colours=new HashSet<Colour>(0);
for(Product p:list)
{
if(p!=null)
{
colours=p.getColours();
}
}
As can be seen, the colours Set is being populated with all of the colour rows available (reference rows) in the product_colour table in Oracle.
After getting all of these rows, I'm getting the entire Colour entity class itself (all the row in it) that corresponds to the colour table in Oracle and then removing those rows which match the rows retrieved from the product_colour Oracle table (available in the colours Set in the preceding snippet) satisfying the condition as mentioned earlier such as,
List<Colour>colourList=session.createQuery("from Colour order by colourId desc").list();
Iterator<Colour>it=colourList.iterator();
while(it.hasNext())
{
Colour c=(Colour)it.next();
for(Colour pc:colours) //colours is available in the preceding snippet.
{
if(c==pc)
{
it.remove();
}
}
}
This can do what is intended but doing so, may imply some overhead on the system. Additionally, what I want to achieve doesn't seem possible with this approach which is pagination. I can't use the setFirstResult(int) and the setMaxResults(int) methods to accomplish the task of pagination which is the case otherwise like the one shown below regarding the Product entity class,
List<Product> products=session.createQuery("from product order by prodId desc")
.setMaxResults(0).setFirstResult(4);
So the question is again, regarding this relationship, is this possible to write such an HQL statement that can retrieve only those rows from the Colour entity class which don't match the colour rows in the product_colour Oracle table like the native SQL statement shown above?
How can I achieve the concept of pagination otherwise (in case, it is not possible)?
Short answer to a veeeeery long question:
select colour from Colour colour
where colour.id not in (
select colour2.id from Product product
inner join product.colours colour2
where product.id = :productId)

How do insert row into Child Table?

I'm fairly new to entity framework and patterns. How would I insert data into a child table?
I inserted the information into the parent table. Do I have to return the identity of the row and then issue another insert?
User Table and User Role.
I've been trying to find a solution for a day now, but I think its because I'm searching the wrong results. My structure is set up using Unit of Work/Repository Pattern.
Any help would be helpful. Thanks!
Get the Parent object. Add the child record to it's navigation property.
something like this
int orderId=33;
var parent=dbContext.Orders.Where(x=>x.Id==orderId);
parent.Details.Add(new OrderDetail{ Quantity=4, ItemId=37});
parent.Details.Add(new OrderDetail{ Quantity=2, ItemId=48});
dbContext.SaveChanges();
Assuming Order is your Master entity and Detail is your child entity and dbContext is your DBContext class object.
EDIT : If you are adding it together,
Order parent=new Order { CustomerId=35, Discount=43.63 };
parent.Details.Add(new OrderDetail{ Quantity=4, ItemId=37});
parent.Details.Add(new OrderDetail{ Quantity=2, ItemId=48});
dbContext.Orders.Add(parent);
dbContext.SaveChanges();

Resources