Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

complete (portable) support for Jakarta Persistence #477

Closed
wants to merge 2 commits into from

Conversation

gavinking
Copy link
Contributor

This proposes an implementation of #470, along with spec language properly specifying how Jakarta Data supports JPA.

====
This release of Jakarta Data does not standardize lifecycle annotations or query annotations for use with Jakarta Data providers backed by Jakarta Persistence.
====
Alternatively, a Jakarta Data provider might support repositories whose associated entities are annotated `jakarta.persistence.Entity` and mapped according to the Jakarta Persistence specification, but whose lifecycle methods are annotated with the Jakarta Data annotations `@Insert`, `@Update`, and `@Delete`. Such a repository is "stateless", in the sense that it does not maintain a persistence context which outlives the invocation of a single repository method. The lifecycle operations of this kind of repository adhere to the usual semantics defined by this specification for the annotations listed. In particular, these operations never cascade to related entities. Such a repository is typically not permitted a resource accessor method of type `jakarta.persistence.EntityManager`, since `EntityManager` is designed for use with stateful persistence contexts.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@njr-11 as promised, this reintroduces the support for stateless sessions.

@otaviojava
Copy link
Contributor

I like this specialization!

Question: Can we have a module specific to ORM?

Instead of putting it at the core, we have a module specific to ORM/JPA.

This way, the scope of the core will still be cross-databased, and a new module/jar will be included in this specialization.

classDiagram
    Core <|-- ORM

Loading

@gavinking
Copy link
Contributor Author

gavinking commented Feb 22, 2024

Can we have a module specific to ORM?

Ah, yeah, sure, we could.

Not sure it's really necessary since—for now at least—it's just six annotations with no dependencies on anything. But I'm not against doing that....

@gavinking
Copy link
Contributor Author

Also, @otaviojava, you need to tell me if you want an interface OrmRepository<T,K> extends DataRepository<T, K>.

@gavinking
Copy link
Contributor Author

Also, @otaviojava, you need to tell me if you want an interface OrmRepository<T,K> extends DataRepository<T, K>.

So here's a wrinkle: if you want such an interface, we would probably need to pick up a dependency to Jakarta Persistence, which would argue in favor of a separate module.

Here's I guess what it would look like, approximately:

/**
 * <p>A built-in repository supertype for repositories backed by a Jakarta Persistence
 * {@code EntityManager}.</p>
 *
 * @param <T> the type of the primary entity class of the repository.
 * @param <K> the type of the Id attribute of the primary entity.
 */
public interface OrmRepository<T, K> extends DataRepository<T, K> {

	@Find
	T find(K id);

	@Find
	T find(K id, FindOption... options);

	@Persist
	void persist(T entity);

	@Persist
	void persistAll(Iterable<T> entities);

	@Remove
	void remove(T entity);

	@Merge
	T merge(T entity);

	@Detach
	void detach(T entity);

	@Refresh
	void refresh(T entity);

	@Refresh
	void refresh(T entity, RefreshOption... options);

	@Lock
	void lock(T entity, LockModeType lockModeType);

	@Lock
	void lock(T entity, LockModeType lockModeType, LockOption... options);

	EntityManager entityManager();

}

@gavinking
Copy link
Contributor Author

Note that I don't personally find that interface very compelling. It's just sort of a worse EntityManager.

*
* SPDX-License-Identifier: Apache-2.0
*/
package jakarta.data.orm;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be called jakarta.data.jpa, since ORM is too generic and broad a term and some of these annotations whilst they look useful are JPA/Hibernate specific

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not happy with "orm" either, but as I have the impression that the Jakarta spec aren't using the name "JPA" anymore. The JPA spec itself doesn't use the term, it's "Jakarta Persistence" everywhere.

(I think this is stupid, FTR, and I'm fine with "jpa".)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the Jakarta Persistence uses the jakarta.persistence, how about:
jakarta.data.persistence

At the end, it is a specialization of Jakarta Data to Jakarta Persistence.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about: jakarta.data.persistence

Fine by me.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JPA is more common for developers, we are familiar with jpa, cdi, etc in Jakarta EE world.

@otaviojava
Copy link
Contributor

Member Author

Also, @otaviojava, you need to tell me if you want an interface OrmRepository<T,K> extends DataRepository<T, K>.

Yeap, once ORM is a specialization of DataRepository.

@otaviojava
Copy link
Contributor

Also, @otaviojava, you need to tell me if you want an interface OrmRepository<T,K> extends DataRepository<T, K>.

So here's a wrinkle: if you want such an interface, we would probably need to pick up a dependency to Jakarta Persistence, which would argue in favor of a separate module.

Here's I guess what it would look like, approximately:

/**
 * <p>A built-in repository supertype for repositories backed by a Jakarta Persistence
 * {@code EntityManager}.</p>
 *
 * @param <T> the type of the primary entity class of the repository.
 * @param <K> the type of the Id attribute of the primary entity.
 */
public interface OrmRepository<T, K> extends DataRepository<T, K> {

	@Find
	T find(K id);

	@Find
	T find(K id, FindOption... options);

	@Persist
	void persist(T entity);

	@Persist
	void persistAll(Iterable<T> entities);

	@Remove
	void remove(T entity);

	@Merge
	T merge(T entity);

	@Detach
	void detach(T entity);

	@Refresh
	void refresh(T entity);

	@Refresh
	void refresh(T entity, RefreshOption... options);

	@Lock
	void lock(T entity, LockModeType lockModeType);

	@Lock
	void lock(T entity, LockModeType lockModeType, LockOption... options);

	EntityManager entityManager();

}

Yes, this was the main reason for proposing the module. Thus, you can go deep into Jakarta's Persistence to create a better user experience without impacting the code.

@gavinking
Copy link
Contributor Author

Yes, this was the main reason for proposing the module. Thus, you can go deep into Jakarta's Persistence to create a better user experience without impacting the code.

Yeah, sure, makes sense. I have no objection to that. Just don't expect me to do Maven things 'cos I'm clueless :-)

@otaviojava
Copy link
Contributor

Yes, this was the main reason for proposing the module. Thus, you can go deep into Jakarta's Persistence to create a better user experience without impacting the code.

Yeah, sure, makes sense. I have no objection to that. Just don't expect me to do Maven things 'cos I'm clueless :-)

I can use your branch as a base to propose this new module.

@gavinking
Copy link
Contributor Author

I can use your branch as a base to propose this new module.

Perfect.

@gavinking
Copy link
Contributor Author

Closing this since #480 was merged instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants