Skip to content

Commit

Permalink
support for queries defined in intermediate classes
Browse files Browse the repository at this point in the history
for our work on the Jakarta Data TCK

Signed-off-by: Gavin King <gavin@hibernate.org>
  • Loading branch information
gavinking committed Apr 2, 2024
1 parent e21d139 commit f51d8db
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.hibernate.processor.test.data.namedquery;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToMany;

import java.util.Set;

@Entity
public class Author {
@Id
String ssn;
String name;

// @Embedded
// Address address;

@ManyToMany
Set<Book> books;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.hibernate.processor.test.data.namedquery;

import jakarta.persistence.Basic;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.Table;
import org.hibernate.annotations.NaturalId;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Set;

@Entity
@Table(name = "books")
public class Book {
@Id
String isbn;

@NaturalId
@Basic(optional = false)
String title;

@Basic(optional = false)
String text;

@NaturalId
LocalDate publicationDate;

@ManyToMany(mappedBy = "books")
Set<Author> authors;

BigDecimal price;

int pages;

public Book(String isbn, String title, String text) {
this.isbn = isbn;
this.title = title;
this.text = text;
}
Book() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.hibernate.processor.test.data.namedquery;

import jakarta.data.repository.Query;
import jakarta.data.repository.Repository;

import java.util.List;

@Repository(dataStore = "myds")
public interface BookAuthorRepository$ extends BookAuthorRepository {
@Override
@Query("from Book where title like :title")
List<Book> findByTitleLike(String title);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.hibernate.processor.test.data.namedquery;

import jakarta.data.Limit;
import jakarta.data.Order;
import jakarta.data.Sort;
import jakarta.data.page.CursoredPage;
import jakarta.data.page.Page;
import jakarta.data.page.PageRequest;
import jakarta.data.repository.By;
import jakarta.data.repository.Delete;
import jakarta.data.repository.Find;
import jakarta.data.repository.Insert;
import jakarta.data.repository.OrderBy;
import jakarta.data.repository.Param;
import jakarta.data.repository.Query;
import jakarta.data.repository.Repository;
import jakarta.data.repository.Save;
import jakarta.data.repository.Update;
import org.hibernate.StatelessSession;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

@Repository(dataStore = "myds")
public interface BookAuthorRepository {
List<Book> findByTitleLike(String title);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.processor.test.data.namedquery;

import org.hibernate.processor.test.util.CompilationTest;
import org.hibernate.processor.test.util.WithClasses;
import org.junit.Test;

import static org.hibernate.processor.test.util.TestUtil.assertMetamodelClassGeneratedFor;
import static org.hibernate.processor.test.util.TestUtil.assertNoMetamodelClassGeneratedFor;
import static org.hibernate.processor.test.util.TestUtil.getMetaModelSourceAsString;

/**
* @author Gavin King
*/
public class NamedQueryTest extends CompilationTest {
@Test
@WithClasses({ Author.class, Book.class, BookAuthorRepository.class, BookAuthorRepository$.class })
public void test() {
System.out.println( getMetaModelSourceAsString( Author.class ) );
System.out.println( getMetaModelSourceAsString( Book.class ) );
System.out.println( getMetaModelSourceAsString( Author.class, true ) );
System.out.println( getMetaModelSourceAsString( Book.class, true ) );
System.out.println( getMetaModelSourceAsString( BookAuthorRepository.class ) );
assertMetamodelClassGeneratedFor( Author.class, true );
assertMetamodelClassGeneratedFor( Book.class, true );
assertMetamodelClassGeneratedFor( Author.class );
assertMetamodelClassGeneratedFor( Book.class );
assertMetamodelClassGeneratedFor( BookAuthorRepository.class );
assertNoMetamodelClassGeneratedFor( BookAuthorRepository$.class );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,10 @@ else if ( element instanceof TypeElement ) {
context.logMessage( Diagnostic.Kind.OTHER, "Processing repository class '" + element + "'" );
final AnnotationMetaEntity metaEntity =
AnnotationMetaEntity.create( typeElement, context );
context.addMetaAuxiliary( metaEntity.getQualifiedName(), metaEntity );
if ( metaEntity.isInitialized() ) {
context.addMetaAuxiliary( metaEntity.getQualifiedName(), metaEntity );
}
// otherwise discard it (assume it has query by magical method name stuff)
}
}
else {
Expand Down

0 comments on commit f51d8db

Please sign in to comment.