Can JpaRepository take entity description from *.hbm.xml files? - rest

I write application using hibernate, spring-boot-data-jpa and spring-boot-data-rest.
My entity classes are not contain any annotations, and all orm mapping placed in several hbm.xml files.
class MyEntity {
Long id;
String name;
}
interface MyRepository extends JpaRepository<MyEntity, Long> {
}
Hibernate working fine, as well as all methods of JpaRepository like findOne. The problem that the rest interface provided by JpaRepository say
"PersistentEntity does not have an identifier property!".
I found that adding #Id to MyEntity class solves the problem. However, I prefer define orm mapping in hbm.xml file, not using annotations.
How can I configure JpaRepository's to consider *.hbm.xml files?

I faced similar problem with JpaRepository using hbm files. JpaRepository can accept hbm files if these are present in resource folder. In resources folder, create any folder say resources/hbm and move all hbm file to this folder. Now hbm files will be found by JpaRepository and not an managed type error will be fixed.

Related

Spring data inject repository without explicit type

I have a service that needs to use Neo4jRepository (regular repository provider by spring data).
public class SomeServiceBean<T>{
#Autowired
private Neo4jRepository<T,Long> Neo4jRepository;
}
This class will generate en error:
expected single matching bean but found 2: systemUserRepository,systemClaimRepository
The problem is that systemClaimRepository and systemUserRepository is extending Neo4jRepository<T,Long> as a bean without implementation.
Spring see systemClaimRepository and systemUserRepository as Neo4jRepository<T,Long> because they are extending it.
Is there anyway to inject Neo4jRepository<T,Long>?
Thanks
No how should this work?
You have two beans that match the interface and Spring does not know which implementation to inject.

How to register custom Querydsl EntityPathResolver with Spring Data MongoRepositoryFactory?

I am using the Querydsl extension (QueryDslPredicateExecutor) to my CrudRepository.
To reliably exclude the generated Q classes from my test coverage measurements, they are generated into a dedicated querydsl subpackage of the respective domain classes (annotation processor option -Aquerydsl.packageSuffix=.querydsl).
Alas, this causes a ClassNotFoundException at application start up:
java.lang.IllegalArgumentException: Did not find a query class org.example.QDomain for domain class org.example.Domain!
at org.springframework.data.querydsl.SimpleEntityPathResolver.createPath(SimpleEntityPathResolver.java:63)
at org.springframework.data.mongodb.repository.support.QueryDslMongoRepository.<init>(QueryDslMongoRepository.java:85)
at org.springframework.data.mongodb.repository.support.QueryDslMongoRepository.<init>(QueryDslMongoRepository.java:67)
…
Caused by: java.lang.ClassNotFoundException: org.example.QDomain
…
I have already located the EntityPathResolver interface that supposedly would allow me to plug in my own domain class to Q class mapping that inserts the .querydsl package suffix, but I haven’t found a way to configure Spring Data’s MongoRepositoryFactory to pick my own EntityPathResolver.
Is this possible?
Currently, the only way is to create your own variant of the MongoRepositoryFactory because the instance of the EntityPathResolver is hard-wired into it.

Why is my projection interface not picked up by Spring Data REST?

I am trying to use up projections with Spring Data REST (version 2.3.0.RELEASE). I read the reference documentation, and gathered that these are the parts I need:
A JPA Entity
#Entity
public class Project implements Serializable {
#Basic(optional = false)
#Column(name = "PROJECT_NAME")
private String projectName;
// ... lots and lots of other stuff
}
A repository that works with that entity
#Repository
public interface ProjectRepository extends JpaRepository<Project, Long> { }
And a projection to retrieve just the name for that entity
#Projection(name="names", types={Project.class})
public interface ProjectProjectionNamesOnly {
String getProjectName();
}
I would like to be able to optionally retrieve just a list of names of projects, and projections seemed perfectly suited to this. So with this setup, I hit my endpoint at http://localhost:9000/projects/1?projection=names. I get back ALL of the attributes and collections links, but I expected to get back just the name and self link.
I also viewed the sample project on projections, but the example is for excerpts, which seems different from projections as it is a different section of the reference. I tried it and it didn't work anyway though.
So the question is this: How do you use spring data rest projections to retrieve just a single attribute of an entity (and its self link)?
Looks like your projection definition is not even discovered and thus it doesn't get applied if you select it for the HTTP request.
For projection interfaces to be auto-discovered they need to be placed inside the very same or a sub-package of the package of the domain type they're bound to.
If you can't put the type into that location, you can manually register a projection definition on RepositoryRestConfiguration by calling ….projectionConfiguration().addProjection(…).
The reference documentation does not really mention this at the moment but there's already a ticket to get this fixed in future versions.

Groovy Mixin persistent properties with JPA

I would like to define a JPA persisted property in a Groovy Mixin and then use it in several entity classes. I couldn't get this to work with JPA annotations and Hibernate - has anyone been successful with this combination?
I have a set up an example Maven project which shows what I'm trying to do and a single JUnit test which defines the behavior I would like.
https://github.com/gilday/groovy-mixin-jpa-test
Briefly:
#Category(Person) class HasPreferences {
#ElementCollection
final Collection<Preference> preferences = []
}
#Entity
#Mixin(HasPreferences)
class Person {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
long id
String name
}
Since #Mixin is dynamic, i doubt JPA will be able to find your mixed properties. I think you need some compile-time code generation, like #Delegate. Even so, JPA will try to persist the generated property. There is a discussion in groovy mailing list concerning the creation of a #Trait annotation which might be what you want.

Meta data on table scaffolding in dynamic data project

I created an ASP.NET webapplication with dynamic data. I'm fairly new to this so I'm struggling with alot of things but now I'm completely stuck.
Thing is, I want to hide, lets say, the name column of a table in my database (model based on entity framework). Therefor I added a new folder named "AppCode" (because I cannot add the default app_code folder in a web app) and added a file named "User.cs" The contents of this file look like this:
[MetadataType(typeof(UserMetaData))]
public partial class User{
}
public class UserMetaData
{
[ScaffoldColumn(false)]
public object Name;
}
Now, after running the application I did not expect to see the name column in the crud pages, but it is still there. What am I missing here?
Thanks alot.
Finally figured it out myself. What went wrong was the fact that my model was placed in a referenced class library and not in the dynamic data project itself. It seems to be very important that the namespace of the partial class is the same as that of the model. Otherwise it would not work. So, in my case I had to place the partial class in my "domain" project which contains the model. Hope this helps someone.