Answer the question
In order to leave comments, you need to log in
How is the connection of models with the database in java / scala applications?
Good afternoon.
It so happened that in almost all applications in the development of which I participated, the interaction in the database was implemented through ActiveRecords. For web projects, this is normal. All the main models contained the logic of work and at the same time were a reflection of the tables from the database.
Now I'm trying to join the world of Java / Scala development. I understand that a different approach is being practiced here. There are models - simple classes containing logic, and there are ORM classes that represent tables from the database. I don't understand how to tie it together.
I messed around with Scala syntax (I was superficially familiar with Java). Decided to make a simple blog. Unfurled the application on PlayFramework2 (standard typesafe template). Everything is clear with controllers, configs, views. But with models plugging.
Added slick. But I can't find a proper tutorial anywhere. I reviewed kure rap on github, 3 different approaches are used everywhere. The examples on their website describe only how to work with the query builder (in its various manifestations). But I can't figure out how to connect normal model classes to the database.
Please, tell me where can I find a description of the main approaches to working with the database through models? Both java and scala will do. I need to understand the essence. Or can you show a repository with a simple application, but in which you can see how the work with the database works from start to finish?
Sorry for the rambling question. I'm just in shock. Everything is new, a lot of things are not clear.
Answer the question
In order to leave comments, you need to log in
In the Java world, there are many ways to organize communication with a database and communication of models with a database in particular. There is a standard - JPA. In the spring world, there is a JPA compatibility layer, and there are other solutions. In Scala, you can use both the above solutions and your own (here it already depends on the framework used).
I will describe how it works in JPA.
First, you describe the connection of the entire application with the database in the persistence.xml file. In it, you describe persistence-units - persistence units, the connection of models with the database. Roughly speaking, you can use both local resources (RESOURCE_LOCAL - communication occurs not through the application server, but through third-party efforts, the connection is described in the xml file) and JTA resources (the connection is configured on the server, in the application you get the desired DataSource by name).
Then you create entity classes. You create a regular (POJO) class with the annotation Entity , describe the fields, getters and setters. With annotations, you can set up all sorts of things: the name of the table in the database, the name of the field, set relationships, type of receipt (LAZY / EAGER), cascading, auto-generation of primary keys, etc.
Then you need to create a class that will provide an interface for working with entities. Usually, for each entity, you need to make your own class. There are several implementations and names of these classes. NetBeans, for example, creates facade classes : one abstract and one abstract-derived facade per entity. The link is clear, I think. Each facade is a bean ( Stateless annotation ). EntityManager is injected into it:
@PersistenceContext(unitName = "AffableBeanPU")
private EntityManager em;
import org.foo.example.entities.Foo;
import org.foo.example.facades.FooFacade;
@Path("foo")
@Consumes({"application/json", "application/xml"})
@Produces({"application/json", "application/xml"})
class FooResource {
@EJB
FooFacade facade;
@GET
public List<Foo> getAll() {
return facade.findAll();
}
@POST
public Foo create(Foo item) {
facade.create(item);
return item;
}
@GET
@Path("{id}")
public Foo getOne(@PathParam("id") Integer id) {
return facade.find(id);
}
@PUT
@Path("{id}")
public Foo update(@PathParam("id") Integer id, Foo item) {
item.setId(id);
facade.update(item);
return item;
}
@DELETE
@Path("{id}")
public void delete(@PathParam("id") Integer id) {
facade.remove(facade.find(id));
}
}
<property name="hibernate.hbm2ddl.auto" value="update" />
so that tables in the database are automatically created (if they do not exist). There are 2 entities in the entitiesRoot<T> root = criteriaQuery.from(entityClass);
, and then in a loop I get the required fields: for (String field : fields) { root.fetch(field); }
. When the query is executed, these fields will be attached to the result. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question