I am using JPARepository, I chose Hibernate Search for implementing search functionality.
Here is link : http://hibernate.org/search/documentation/getting-started/
Using an EntityManager (JPA) to rebuild an index
FullTextEntityManager ftem = Search.getFullTextEntityManager(entityManager);
ftem.createIndexer().startAndWait();
Suppose I have Two Entity classes Company.java & Employee.java.
Employees(e_id) saved under Company(c_id) and relation stored in another table "company_employee".
I want to implement search on only those employees(not all employees of other companies) which are associated with company account.
for eg: select e_id from company_employee where c_id = ?
Now assume i getting 3 employees of company and want to fetch the all record of these 3 employees and not others.
You fetch company and not employee like this :
QueryBuilder qb = fullTextEntityManager.getSearchFactory()
.buildQueryBuilder().forEntity(Company.class).get();
In your lucene query :
luceneQuery = queryBuilder.keyword().wildcard().onFields("employees.employeeName").matching("m*").createQuery()
Employees is the field annotatted with #IndexedEmbedded.
public class Company {
...
#IndexedEmbedded
private List<Employee> employees;
...
}
You will get a company with only its employees and not all the employees. (I don't know if you have to use eager fetch or not)
Is that what you are looking for ?
Related
I would like to retrieve the list of requested entities from a JPQL request
and if I detect a specific one add a filtering criteria
sample
String jpql = "select u.fname, u.lname from **Users** u JOIN u.group g where g.name = :groupName";
here the request will work on entity "Users" and entity "Group"
so I would like to retrieve the list of entityType (Users / Group) or Type User.class, UserGroup.class
and to do a specific filtering in case I found a sensitive table
(not all Group are public and sometimes the personnel that create the query forget to apply the filtering)
I am little confused, If i have two tables related then I will have a combined table in MySQL, Since we do not have a class in our project , how would my Typed Query look in order to fetch data from table that is create from Relationship( say One to Many).
eg:
#OneToMany(cascade = CascadeType.ALL)
#JoinTable(name = "CustomerBilling",joinColumns = #JoinColumn(name = "Customer_Id"),
inverseJoinColumns = #JoinColumn(name = "Billing_Id"))
private List<Billing> billing = new ArrayList<>();
with the above mentioned code i will have CustomerBilling table , So i would like to get all the records for a particular customer id. in my test (jUnit) file what Typed Query do i need to put ?
TypedQuery<Customer> a = em.createQuery("select b from CustomerBilling b where b.Customer_Id =?1", Customer.class);
This did not work Since CustomerBilling abstrate schema is not present.
Thanks
Prashanth
I am sure you have classes in your project as it is java based :)
Reading between the lines, you mean, that you have 3 tables in DB Customer, Billing and CustomerBilling, but only 2 entities in JPA the Customer and Billing as the CustomerBilling is there only to store the relation. (But your example of typed query tries to get Customer.class as the result so maybe I am wrong).
You don't reference jointable objects directly (unless they are mapped).
You can query by Customer, or by Billing but not by CustomerBilling. If there is no such entity defined in JPA, there is nothing that JPA can query about.
And you shouldn't try to do it, even for unit testing (BTW jpa tests are integration tests not unit tests).
The whole point of mapping the relationships with #OnteToMany and such is to hide, from the program, the actual DB structure. So you should access Billings of a customer, or customer for a billing, not the relation info.
Currently the relation is stored by join table, but it could be change it to join column and whole program logic would stay the same (and so should the tests).
You should test, that if you add bilings to customer and save them, you correctly retrieves them, that if you remove the biling from customer it disappears, but there is no reason to check the content of the join table.
Finally, if you really must, you can use native sql query to access CustomerBilling table
em.createNativeQuery
or
you can change your mappings, and introdude the CustomerBilling entity, and map your OneToMay on Customer to the CustomerBilling instead of Directly Billing.
I have a table named product having product id, product bill id and.. the bill id is passed to my controller as a parameter.
I can use the entities from framework and find all rows with product id using
db.tbl_product.Find(product id).
But now i need to find all transactions using bill id. How do I do that??
Assuming tbl_product is a DbSet<Product> or something similar, you should be able to use LINQ to query the DbSet. To find a single item with a specific BillId property value, you would do something like this:
var product = db.tbl_product.FirstOrDefault(p => p.BillId == billId);
If there were multiple products with the same BillId, you could do the following:
var products = db.tbl_product.Where(p => p.BillId == billId);
It largely depends on the schema of the table and how you're using Entity Framework. I would highly recommend reading a book or tutorial on Entity Framework. There are lots of them out there, for example: Entity Framework Tutorial.
I am trying to understand what would be the better way to design 2 entities which has many-to-many relationship? In database there will be a connecting table between these two entities. But do i have to follow the same approach while creating my entities?
For example: User to User group
A user may belong to many group and a group may contain many user.
In relational database I will have 3 table like User, User2Group, Group
So when I am creating my JPA entities, should I have 3 entities for 3 table or just 2 entities by providing the proper annotation(#ManytoMany).
Since I am new to JPA, I am trying to understand good and bad side from following point of view:
Performance
Code maintenance
Thanks, you input will be greatly appreciated.
No, you don't need to map the join table as an entity. Just use the ManyToMany annotation:
public class User {
...
#ManyToMany
private Set<Group> groups;
}
public class Group {
...
#ManyToMany(mappedBy = "groups")
private Set<User> users;
}
You would only need to map the join table as an entity if it was not a pure join table, i.e. if it had additional information like, for example, the date when the user entered in the group.
The mapping has little incidence on the performance. What is important is how you use and query the entities, and how the database is designed (indices, etc.)
I have an entity that has a relationship with another entity. I am able to search on columns that are in the main entity, and include columns from the relationship entity. But I need to be able to filter the list (search) on columns that are not in the relationship entity.
for example
the Invoice Entity contains a customerId property, and is related to the Customer Entity which contains the customerName property
I need to be able to search / filter the grid by customerName.
I am new to entity framework, please help.
thanks
Carl
Your relation is 1->1. In these cases I usually return a custom class to the grid that has all the columns I need, including joins with other tables.
So basically what you need is to create a custom linq query with your resultset.
The mais query should follow this example:
var q = from i in ctx.Invoices
join c in ctx.Customers on i.CustomerID equals c.CustomerID
select new{InvoiceID=i.InvoiceID, InvoiceDate=i.Date, CustomerName=c.Name};
Now, assuming we receive a CustomerName variable with the string to filter by c.Name we could do:
if(!string.IsNullOrEmpty(CustomerName))
{
q = q.where(c => c.Name.ToLower().Contains(CustomerName.ToLower()));
}
Notice that I'm performing a ToLower() operation and a Contains, this will beahave as a LIKE ingnoring case sensitivity and searching for the string anywhere in the Customer Name.
At the end you'll return the q.ToList() serialized for the jqGrid...