Persisting Entities Mapped to MySQL View in JPA/Hibernate - mysql

I have a entity Customer, which is mapped to a view which is defined as a series of joins between Customer, Account, and Person in my database.
I am using JSF 2.0 with JPA set up with Hibernate. The backing database is MySQL
My question is that given the fact that my Customer entity is mapped to a database view, how will this complicate persisting of new Customer entities?

I quote from Adam Bien's book :
"For SQL queries there is no difference between views and tables, so you can easily map a JPA entity to a view transparently. The code on the Java side remains clean and simple – and you will even get better performance. There is a drawback: not all views are updatable. Whether a view is updatable or not highly depends on the complexity and particular database. E.g. in Derby DB all views are not updatable."
http://www.dzone.com/links/r/mapping_jpa_entities_to_sql_views_it_works_even_w.html

Related

Laravel, what's the best practice to define Raw queries?

I'm crafting a Laravel App that will perform data synchronization from DB to DB. Using MySQL.
I have set up a set of SQL raw statements that will perform some data extraction across tables and using MySQL functions.
I don't have then a 1:1 relationship in order to relate Model to table (Common Eloquent approach)
What's the suggested approach to have things set up in a clean manner?
Store SQL statements as strings in a config file?
Issue virtual model Classes having the raw query inside as class constant?
Any best practice approach in Laravel flavour?

How to write JPA native query to join two tables. Eclipseink JPA,Glassfish3.x,EJB3.1, MySQL

What would be the correct way writing JPA native queries with joins and with Eclipselink JPA2.0 entities?
Can some one tell me to execute the following SQL query using Eclipselink JPA native queries or JPQL?
SQL query is:
Select e.EmployeeName,e.EmpId, d.DeptId from Employee e, Department d join e.DeptId on d.DeptId where e.DeptId=d.DeptId
JPA Entity bean code snippet
#Entity
#Table(name = "employee")
#JoinColumn(name = "DeptId", referencedColumnName = "DeptId")
#ManyToOne
private Department DeptId;
#XmlRootElement
#NamedQueries({
#NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e")})
//more auto generated named queries using NetBeans
The JPA entity class "Employee" implements java.io.Serializable and the default constructor contains all mandatory fields.
What is the correct way to map jpa entity with My-SQL tables
Many to one relation: Employee.DeptId --> Department.DeptId Employee.RoleId --> JobRoles.RoleId
Employee.RoleId --> Skills.SkillId
After succeeded with two tables I want to join three tables.
I was going thru the Ecipselink user-guide but could not get it.
Would some one provide me code snippets or direct me to correct resource.
I have gone through EclipseLink JPA online guide:
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA
I am using native query because Eclipse link JPA supports it and JPQL does not give complete portability.
I wrote and developed a sample jsf-jpa application with jpa entities mapped to single table. With Netbeans IDE, it did not take much time generate and execute JPQL queries.
I want to use native queries and/or stored procedures execute JPA entities.
It would be more productive if I could use my existing skills in SQL queries and stored procedures provided the JPA implementation does not give much issues with the same.
Update:
Native SQL queries can be used according to Section 3.8.15 SQL Queries of JSR 317: [JavaTM Persistence API, Version 2.0][1]
http://download.oracle.com/otndocs/jcp/persistence-2.0-fr-oth-JSpec/
The SQL query facility is intended to provide support for those cases
where it is necessary to use the native SQL of the target database in
use (and/or where the Java Persistence query language cannot be used).
Native SQL queries are not expected to be portable across databases
Hence I believe it is nothing wrong to use native query, because portability is not required for me.
The native query executed successfully, The Section 3.8.15 of JSR 317 specification is helpful to understand the syntax, besides hit and trials with my code.

Store mongoid in MySQL or store MySQL record id in mongo?

For the project we have a MySQL database. We want to use Mongo'S GridFS to store screenshots of each piece of software.
We're not sure if we should store the MySQL software id in the mongo file collection or to store the mongo id in a MySQL table i.e. table screenshots would have software_id and mongo_id. MongoID would point to the collection of the screenshots.
We'll be using Doctrine ORM and Doctrine ODM in parallel.
Any ideas? What would be the best solution? In terms of synchronisation. Would we run into any problems?
Thanks
Assuming each sotware has many screenshots and each screenshot belongs to one software, I would put MySQL ids in MongoDB:
it will avoid having an additional table in MySQL referencing entities it
doesn't own
it should avoid some lookups too (when you need screenshots, you will only
request it with the software id in MongoDB instead of asking MySQL
first and MongoDB afterward)

How does EF check if the database is latest version?

I know EF checks the EdmMetadata table to determine if the version of model classes is same as database tables.
I want to know exactly how EF can find if the version of a model has changed. In other words, I want to know what does EF compare to the modelhash in the database?
Have a look at this blog post about the EdmMetadata table.
For your question, this is the relevant parts:
The EdmMetadata table is a simple way for Code First to tell if the
model used to create a database is the same model that is now being
used to access the database. As of EF 4.1 the only thing stored in the
table is a single row containing a hash of the SSDL part of the model
used to create the database.
(Geek details: when you look in an EDMX file, the SSDL is the part of
that file that represents the database (store) schema. This means that
the EdmMetadata model hash only changes if the database schema that
would be generated changes; changes to the conceptual model (CSDL) or
the mapping between the conceptual model and the database (MSL) will
not affect the hash.)

Entity Framework expose several databases as a whole

I have three databases with exactly the same schema (SAP Business One databases). In this databases I have an item masters table connected to a warehouse stock table via the item code. Can I have just one Entity framework model that has only one item master object and one warehouse stocks object which draws data from the 3 databases?
The items are the same in the three databases but they have different warehouse codes.
I don't know if I have made myself clear.
If you want single EF model which will simultaneously load data from three databases then answer is no. If you want single EF model which can be used for all three databases the answer is yes but all your databases must use same database provider (server) and must have exactly the same schema of mapped tables.
The whole magic in this case is in connection string which can connect only to single database and cross database calls are not allowed.
If you need the first scenario you can try to hide unions and cross database queries in views and map those views in your model. This have two disadvantages:
Relation between views are not allowed in SQL Server but you can create the relation in EF model
Views are read only in EF model. If you want to modify data the best way is mapping stored procedures which will do that.