How to map calculated properties with JPA and Hibernate

JPA doesn’t offer any support for derived property so you’ll have to use a provider specific extension. As you mentioned, @Formula is perfect for this when using Hibernate. You can use an SQL fragment: @Formula(“PRICE*1.155”) private float finalPrice; Or even complex queries on other tables: @Formula(“(select min(o.creation_date) from Orders o where o.customer_id = id)”) private … Read more

What is the difference between persist() and merge() in JPA and Hibernate?

JPA specification contains a very precise description of semantics of these operations, better than in javadoc: The semantics of the persist operation, applied to an entity X are as follows: If X is a new entity, it becomes managed. The entity X will be entered into the database at or before transaction commit or as … Read more

What’s the use of session.flush() in Hibernate

Flushing the session forces Hibernate to synchronize the in-memory state of the Session with the database (i.e. to write changes to the database). By default, Hibernate will flush changes automatically for you: before some query executions when a transaction is committed Allowing to explicitly flush the Session gives finer control that may be required in … Read more

flask-sqlalchemy or sqlalchemy

The main feature of the Flask-SQLAlchemy is proper integration with Flask application – it creates and configures engine, connection and session and configures it to work with the Flask app. This setup is quite complex as we need to create the scoped session and properly handle it according to the Flask application request/response life-cycle. In … Read more

How to annotate MYSQL autoincrement field with JPA annotations

To use a MySQL AUTO_INCREMENT column, you are supposed to use an IDENTITY strategy: @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; Which is what you’d get when using AUTO with MySQL: @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; Which is actually equivalent to @Id @GeneratedValue private Long id; In other words, your mapping should work. But Hibernate should omit … Read more

What is Persistence Context?

A persistence context handles a set of entities which hold data to be persisted in some persistence store (e.g. a database). In particular, the context is aware of the different states an entity can have (e.g. managed, detached) in relation to both the context and the underlying persistence store. Although Hibernate-related (a JPA provider), I … Read more

Why do we need entity objects? [closed]

I think it comes down to how complicated the “logic” of the application is, and where you have implemented it. If all your logic is in stored procedures, and all your application does is call those procedures and display the results, then developing entity objects is indeed a waste of time. But for an application … Read more

Filter by property

Nope. Django filters operate at the database level, generating SQL. To filter based on Python properties, you have to load the object into Python to evaluate the property–and at that point, you’ve already done all the work to load it.

SQLAlchemy: how to filter date field?

In fact, your query is right except for the typo: your filter is excluding all records: you should change the <= for >= and vice versa: qry = DBSession.query(User).filter( and_(User.birthday <= ‘1988-01-17’, User.birthday >= ‘1985-01-17’)) # or same: qry = DBSession.query(User).filter(User.birthday <= ‘1988-01-17’).\ filter(User.birthday >= ‘1985-01-17’) Also you can use between: qry = DBSession.query(User).filter(User.birthday.between(‘1985-01-17’, ‘1988-01-17’))

JPA CascadeType.ALL does not delete orphans

If you are using it with Hibernate, you’ll have to explicitly define the annotation CascadeType.DELETE_ORPHAN, which can be used in conjunction with JPA CascadeType.ALL. If you don’t plan to use Hibernate, you’ll have to explicitly first delete the child elements and then delete the main record to avoid any orphan records. execution sequence fetch main … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)