ebean does not generate methods for inhereted annotated fields? - annotations

This works:
#MappedSuperclass
public class ExtendableTranslation extends Model {
public String label;
}
#Entity
public class LeagueT extends ExtendableTranslation {
#Id
public Integer id;
}
but if I move id field to the superclass:
#MappedSuperclass
public class ExtendableTranslation extends Model {
public String label;
#Id
public Integer id;
}
#Entity
public class LeagueT extends ExtendableTranslation {
}
I have an error:
[RuntimeException: java.lang.NoSuchMethodError: models.translations.LeagueT._ebean_setni_id(Ljava/lang/Integer;)V]
It's the same for all other annotated fields (#ManyToOne, #OneToMany). Is it a bug or I'm doing something wrong?

Related

Build method in JPA with composite key

This is my entity with composite key:
#Entity
#Table(name = "course_ratings")
public class CourseRating {
#EmbeddedId
private CourseRatingKey id;
}
Where CourseRatingKey looks like this:
#Embeddable
public class CourseRatingKey implements Serializable {
#Column(name = "user_id")
private int userId;
#Column(name = "course_id")
private int courseId;
public CourseRatingKey() {
}
// getters, setters, equals(), hashCode()
}
And my JPA repository
#Repository
public interface CourseRatingRepository extends JpaRepository<CourseRating, CourseRatingKey> {
}
I am trying to build method that will return list of all CourseRating with given courseId property of CourseRatingKey. Below method doesn't work because JPA doesn't recognize it:
repository.findAllByIdCourseId(int id);
How can I build my method name to achieve my goal?
I have solved my problem by declaring this method in repository. I'm a bit confused as the other methods work without declaring.
#Repository
public interface CourseRatingRepository extends JpaRepository<CourseRating, CourseRatingKey> {
List<CourseRating> findAllByIdCourseId(Integer id);
}

Spring Boot Mongo DB Inheritance

Class A{
public String phone;
}
#Document
Class B extends A{
public String location;
///getter and setter
}
#Repository
public interface B extends MongoRepository<B, String> {
List<B> findByphone(String wphone);
}
When using this findByphone(phone) my api saying this
"status": 500,
"error": "Internal Server Error",
"message": "No property 'phone' found on class com.example.demo.model.B! Did you mean: phone,Phone?"
Try using #Field in your mongodb fields and also you need to have an #Id associated with each of your document
Class A {
#Field
public String phone;
// getters and setters
}
#Document(collection = "B")
Class B extends A {
#Id
private String id;
#Field
public String location;
// getter and setter
}
Also in your repository interface use the method like
List<B> findByPhone(String wphone);

JPA : Entity extend with entity

How can I extend an entity with another entity but both of them referring to the same table ? Is it possible ? The structure is something like this :
#Entity
#Table(name = "users")
#NamedQuery(name="User.findAll", query="SELECT u FROM User u")
public class User implements Serializable{
private int id;
private String name;
}
#Entity
#Table(name = "users")
#NamedQuery(name="SubUser.findAll", query="SELECT su FROM SubUser su")
public class SubUser extends User {
#Override
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
public int getId() {
return super.getId();
}
//- Other fields and getter setter
}
I tried this way Extend JPA entity to add attributes and logic
but I got this exception
org.hibernate.mapping.SingleTableSubclass cannot be cast to org.hibernate.mapping.RootClass
Update 1
I already put the #Id for the SubUser because the #Entity shows this exception
The entity has no primary key attribute defined
Add the #Inheritance annotation to the super class
Implement Serializable
Add a getter for id (you don't need a setter necessarily)
id should be Integer, not int, so that you can represent unassigned ids with null.
Code:
#Entity
#Table(name = "users")
#Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class User implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String name;
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
#Entity
public class SubUser extends User {
}
Any basic JPA docs would describe inheritance, discriminators and use of #Id.
#Entity
#Inheritance(strategy=InheritanceType.SINGLE_TABLE)
#DiscriminatorColumn(name="DISCRIM", discriminatorType=DiscriminatorType.STRING)
#DiscriminatorValue("User")
#Table(name="users")
#NamedQuery(name="User.findAll", query="SELECT u FROM User u")
public class User implements Serializable{
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
}
#Entity
#DiscriminatorValue("SubUser")
#NamedQuery(name="SubUser.findAll", query="SELECT su FROM SubUser su")
public class SubUser extends User {
}

How to declare a base repository for a MappedSuperclass base type with spring data?

We're having a hierarchy of entity classes where all inherit from a BaseEntity #MappedSuperclass. Is there a way to have a spring-data "base repository" that allows you to query for any BaseEntity without having to know it's type and use a specific repository?
#MappedSuperclass
public class BaseEntity {
#Id
private Long id;
private String owner;
// setter/getter etc...
}
#Entity
public class FooEntity extends BaseEntity {
private String foo;
}
#Entity
public class BarEntity extends BaseEntity {
private String bar;
}
/**
* How to declare this base repository....
*/
public interface BaseRepository extends JpaRepository<BaseEntity, ActionPK> {
List<BaseEntity> findByOwner(String owner);
}
#Component
public class MyService {
#Autowired
private BaseRepository baseRepository;
public void doSomething() {
// ... so that is can be used like this:
List<BaseEntity> entities = baseRepository.findByOwner("john");
doSomethingWith(entities);
}
}
I think the only way you could achieve this is to create a Service class that will orchestrate all the calls to your Repository classes that have an owner property. That service call would return your list of BaseEntity objects.

Exception thrown with OneToMany annotation

In my Play 2.0 application using the EBean ORM I have the following class:
#Entity
public class User extends Model {
#Id
public Long id;
#Constraints.Required
public String someString;
#OneToMany(mappedBy="user", cascade=CascadeType.REMOVE)
#OrderBy("index")
public List<UserImage> userImages = new ArrayList<UserImage>();
}
Unless I comment out the #OneToMany line completely, the application throws a RunTimeException stating
Error reading annotations for models.User
The UserImage class I refer to here looks like this:
public class UserImage extends Model {
#Id
public long id;
#Constraints.Min(0)
public int index;
#Column(name="user_id")
#ManyToOne
public User user;
//...
}
What am I doing wrong here? Why doesn't EBean understand my annotation?
I think you missed the #Entity annotation on the UserImage class:
#Entity
public class UserImage extends Model {
#Id
public long id;
#Constraints.Min(0)
public int index;
#Column(name="user_id")
#ManyToOne
public User user;
//...
}