How to autogenerate a Panache entity - jpa

Is it possible to auto generate Panache entities from existing database tables in eclipse/any other environment.?
I am trying out quarkus, and have a database with a number of tables and would like to auto generate the entity code

Panache entities are JPA entities with an extends PanacheEntity or extends PanacheEntityBase at the type decaration site.
You can generate your JPA entities, both Eclipse and ItelliJ IDEA have plugins for that, then add the extends clause.
Be carefull that if you extends PanacheEntity you should use the default id strategy that it provides (an autogenerated Long id), so better extends PanacheEntityBase if you generates from an existing schema.
Panache also provides a repository approach that can be useful if you don't want to update your entity after generation.

Related

Spring data Dynamic projection without creating projection dto/interface

I use Spring data by extending SimpleJpaRepository, Sometimes we need only a few special fields of an entity on sometimes other fields. if we create a projection class or interface for every need, there will be many classes that are used only for one application. is there any way to pass fields/columns as map/list to createQuery ?
I use Spring data by extending SimpleJpaRepository
That is at least weird, if not wrong. you'd normally extend on or multiple of Spring Data interfaces.
Anyway, yes this is possible like so:
Is there a way to achieve this?
Yes, there is.
Version 2.6 RC1 of Spring Data JPA introduced fluent APIs for Query By Example, Specifications, and Querydsl.
This you can use among other things to configure projections. Note that only interface projections are supported.
You can use projections like this:
interface SomeRepository extends CrudRepository, JpaSpecificationExecutor{}
MyService {
#Autowired
SomeRepository repository;
void doSomething(){
List<User> users = repository.findBy(
someSpecification,
q -> q.project("firstname", "roles").all()
);
// ...
}
}
It will return an entity, but only the fields given in the project clause will be filled.

How can i get the reference objects in spring boot with mongodb

When we use SQL with Spring boot, we can use hibernate and add #OneToMany relationships. It helps us to get the reference objects from another entity.
As an example, suppose Order and OrderDetails entities. When I query the Order entity, It automatically maps with the OrderDetails entity and it brings the whole document together. How can I do such a thing with Springboot+mongodb? Is there any easy way to solve this problem? Is it called ORM?

Trying to use Crate.io NoSql database with an existing Spring Data / Mysql project

I'm attempting to add Crate.IO capability to an existing Spring Data/Eclipselink/MySql web application. For this specific use case, we want to persist data to both MySql AND Crate (for evaluation purposes) in the most painless way possible. I'm using the Spring-Data-Crate project in order to be able to use Spring Data Repositories with Crate.
I've been able to setup a separate Crate specific entity manager with a filter to only utilize repos that implement CrateRepository. The problem I'm having is determining how to use the existing Spring Data/MySql entity classes with Crate. (or derive from them)
1) If I annotate an existing Spring Data #Entity class with the Spring-Data-Crate
#Table annotation, the mapping to the crate DB will fail because EclipseLink/JPA adds hidden persistence fields to entities objects that start with an underscore, which is apparently not allowed by the spring-data-crate adapter
2) I tried to use entity inheritance, with a base class that both the MySql and Crate entity can extend, with only the MySql entity having the spring data #Entity annotation. Unfortunately, this causes Spring Data to lose visibility of the base class fields unless the base class is annotated with #MappedSuperClass. But adding this annotation introduces the hidden "_"-prefixed persistence properties to the derived crate entity.
3) I could use separate entities entirely and have them implement a common interface, but I can't assign the interface as the type of the spring data crate repository.
... Not sure where to go from here
Spring Data Crate adapter project - https://github.com/KPTechnologyLab/spring-data-crate
Spring Data Crate Tutorial - https://crate.io/a/using-sprint-data-crate-with-your-java-rest-application/
i'm johannes from crate.
we didn't test the use of spring data crate in that manner so we can't state any information if this should or shouldn't work.
sorry, johannes

What is the exact meaning of the JPA #Entity annotation?

I am studying JPA in Spring application and I have some doubts related to the #Entity annotation.
So I have a model class like this:
#Entity
#Table(name= “T_CUSTOMER”)
public class Customer {
#Id
#Column(name=“cust_id”)
private Long id;
#Column(name=“first_name”)
private String firstName;
#Transient
private User currentUser;
...........................
...........................
...........................
}
Ok, I know that the #Entity annotation is on the class level and it means that the fields of the object that are instances of this class are to be mapped to the field of the T_CUSTOMER database table.
But why in JPA it is mandatory to use #Entity annotation and I cannot only use the #Table annotation to map a model object to a specific database table? It have some other meaning\behavior that actually I am missing?
What am I missing? What is the exact meaning of the #Entity annotation?
#Entity annotation defines that a class can be mapped to a table. And that is it, it is just a marker, like for example Serializable interface.
And why #Entity annotation is mandatory? ... well, it is the way how JPA is designed. When you create a new entity you have to do at least two things
annotated it with #Entity
create an id field and annotate it with #Id
Anything else is optional, for example table name is derived from entity class name (and therefore #Table annotation can be optional), table's columns are derived from entities variables (and therefore #Column annotation can be optional), and so on ...
JPA is trying to provide a fast and easy start to developers who want to learn/use this API, and giving developers option to configure as few things as possible to make something functional is one of the ways how this API wants to achieve this "easy to use/learn" goal. Hence the #Entity annotation (together with #Id annotation) is the minimum you have to do in order to create an entity.
Entities in JPA are nothing but POJOs representing data that can be persisted to the database. An entity represents a table stored in a database. Every instance of an entity represents a row in the table.
More about the entities:
https://www.baeldung.com/jpa-entities
Entities represent persistent data stored in a relational database automatically using container-managed persistence.They are persistent because their data is stored persistently in some form of data storage system, such as a database: they do survive a server failure, failover, or a network failure. When an entity is reinstantiated, the state of the previous instance is automatically restored.
An entity models a business entity or multiple actions within a single business process. Entities are often used to facilitate business services that involve data and computations on that data. For example, you might implement an entity to retrieve and perform computation on items within a purchase order. Your entity can manage multiple, dependent, persistent objects in performing its tasks.
Entities can represent fine-grained persistent objects, because they are not remotely accessible components.
An entity can aggregate objects together and effectively persist data and related objects using the transactional, security, and concurrency services of a JPA persistence provider.

How to set id key to be generated by the database in Entity Framework after model creation using Database First

I am using Entity Framework in a Web API project. I have created my classes and models from an existing Database (MySQL), so I basically used the EF DbContextGenerator to generate my classes from my EDMX model.
Read operations are working fine, but I am now at the point where I want to start adding functionality to add records to the database. I want the id for entities to be automatically assigned by the database.
It seems like when you are following a code-first approach, one simply needs to specify:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
In the class definition for the Id property. So I am tempted to simply add the "DatabaseGeneratedOption" to the class file that was generated. However, since I am using a database-first approach and my classes are basically auto-generated for me, I am not supposed to edit the class files as they will get overwritten again should I re-generate the classes again. Where/How do I set the Id value to be generated by the database rather than by the EF code?
It's an option in the properties of a property (o_O) in the edmx designer: