Spring Boot Mongo DB Inheritance - mongodb

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);

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);
}

Picketlink with custom model and long Id

I have a existing Model and want to use it with Picketlink. But I am using Long as #Id field. But Picketlink expect this to be a String field. I have found some hints to use another entity which maps to the corresponding entity of my model. But actually I don't now how to do it.
I have a base class, which all entities derive from:
#MappedSuperclass
public abstract class AbstractEntity implements Serializable, Cloneable {
#Id
#Identifier
#Column(name = "SID")
private Long sid;
#Column(name = "INSERT_TIME")
private Date insertTime;
#Column(name = "UPDATE_TIME")
private Date updateTime;
// getters and setters
}
And a derived realm entity:
#Entity
#IdentityManaged(Realm.class)
public class RealmEntity extends AbstractEntity {
#AttributeValue
private String name;
#PartitionClass
private String typeName;
#ConfigurationName
private String configurationName;
#AttributeValue
private boolean enforceSSL;
#AttributeValue
private int numberFailedLoginAttempts;
// getters and setters
}
And the mapping class for Picketlink looks as follows:
#IdentityPartition(supportedTypes = {
Application.class,
User.class,
Role.class
})
public class Realm extends AbstractPartition {
#AttributeProperty
private boolean enforceSSL;
#AttributeProperty
private int numberFailedLoginAttempts;
private Realm() {
this(null);
}
public Realm(String name) {
super(name);
}
}
The PartitionManager is defined as follows:
builder
.named("default.config")
.stores()
.jpa()
.supportType(User.class, Role.class, Application.class, Realm.class)
.supportGlobalRelationship(Grant.class, ApplicationAccess.class)
.mappedEntity(App.class, AppUserRole.class, AppRole.class, AppUser.class, UserEntity.class, RelationshipIdentityTypeEntity.class, RealmEntity.class)
.addContextInitializer((context, store) -> {
if (store instanceof JPAIdentityStore) {
if (!context.isParameterSet(JPAIdentityStore.INVOCATION_CTX_ENTITY_MANAGER)) {
context.setParameter(JPAIdentityStore.INVOCATION_CTX_ENTITY_MANAGER, entityManager);
}
}
});
When I try to create a new Realm Hibernate throws an error while trying to load the Realm because the #Id is defined as Long but the #Identifier of the Picketlink model is a String.
this.shsRealm = new Realm(REALM_SHS_NAME);
this.shsRealm.setEnforceSSL(true);
this.shsRealm.setNumberFailedLoginAttempts(3);
this.partitionManager.add(this.shsRealm);
java.lang.IllegalArgumentException: Provided id of the wrong type for class de.logsolut.common.picketlink.model.RealmEntity. Expected: class java.lang.Long, got class java.lang.String
How can I map the JPA model correctly to Picketlink?

#ManyToOne conflicts with JpaRepository

#ManyToOne or #OneToOne will replace the joined column with an entity, but how would I do when I want to use JpaRepository to query by that joined column? There's seems a conflict.
#Entity
public class Type {
private Long id;
}
#Entity
public class Item {
#ManyToOne
#JoinColumn(name = "type_id")
private Type type;
}
public interface ItemRepository extends JpaRepository<Item, Long> {
List<Item> findByTypeId(Long typeId);
}
It throws error "property typeId not found", which makes sense because it's surely not in the class "item", instead an entity "type". But how could I query Item by type_id in this situation? And if I declare property typeId, it will throw error duplicated mapping for column "type_id".
You have to do it with _ like
public interface ItemRepository extends JpaRepository<Item, Long> {
List<Item> findByType_Id(Long typeId);
}

ebean does not generate methods for inhereted annotated fields?

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?

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;
//...
}