Here are my entities ForfaitGenerique and Offre . Those two entities are in persistence.xml (didn't put all the methods here, if needed I will add more information):
#Entity
public class ForfaitGenerique implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int idForfait;
#NotNull
private String description = "description";
#NotNull
#OneToMany(cascade = {CascadeType.ALL},fetch = FetchType.LAZY,mappedBy = "forfaitGenerique")
private List<Offre> listeOffre;
#NotNull
#ElementCollection
List<Integer> listeRemontees;
//erreur sur mon intellij mais pas d'erreur en faisant mvn clean install.On verra au test
public ForfaitGenerique() {
}
public void addOffre(Offre o) {
this.listeOffre.add(o);
}
[...]
#Entity
public class Offre implements Serializable {
#NotNull
private AgeEnum age;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
#NotNull
private double nbJour;
#Enumerated(EnumType.STRING)
#NotNull
private SaisonEnum saison;
#Enumerated(EnumType.STRING)
#NotNull
private ReductionEnum reduction;
boolean consecutif;
#NotNull
private double prix;
#ManyToOne
private ForfaitGenerique forfaitGenerique;
public Offre() {
}
Here is what I am trying to persist a ForfaitGenerique ; when I persist it and then try to retrieve it through its id (which isn't null and seems to have a good value), I get a ForfaitGenerique, which is not null, but ALL stuff it should contain is null (not the id though) :
#EJB(name="databaseAccess") protected ForfaitsInterface databaseAccess;
private ForfaitGenerique f;
private Offre offre_f;
[...]
ArrayList<Integer> l = new ArrayList<>();
l.add(1);
l.add(2);
l.add(3);
ForfaitGenerique forf = new ForfaitGenerique();
List<Offre> l_o = new ArrayList<Offre>();
forf.setListeOffre(l_o);
forf.setListeRemontees(l);
Offre o = new Offre(forf,AgeEnum.ADULTE,0.5,SaisonEnum.HAUTE,ReductionEnum.FIDELICIME,true,12.0);
forf.addOffre(o);
databaseAccess.addForfaitGenerique(forf);
int id_f = forf.getIdForfait();
assertNotNull(id_f);
System.out.println("bloublou"+id_f);
f = databaseAccess.getForfaitGenerique(id_f);
assertNotNull(f);
assertNotNull(f.getListeRemontees());//null !!
assertNotNull(f.getListeOffre());//null !!
assertEquals(f,forf);
offre_f = f.getListeOffre().get(0);
The databaseAccess object contains an entitymanager and two methods (among other), which are :
#Stateless(name="databaseAccess")
public class Forfaits implements ForfaitsInterface {
#PersistenceContext private EntityManager entityManager;
public void addForfaitGenerique(ForfaitGenerique forfaitGenerique) {
entityManager.persist(forfaitGenerique);
System.out.println("contains = "+entityManager.contains(forfaitGenerique));
}
#Override
public void addOffre(Offre o) {
entityManager.persist(o);
}
public ForfaitGenerique getForfaitGenerique(int id_forfait) {
ForfaitGenerique f = entityManager.find(ForfaitGenerique.class,id_forfait);//database.getForfaitFromId(id_forfait);
return f;
}
I think that my problem is when I am trying to persist my object, but not sure . Any help is appreciated .
In fact I just forgot to add the #Transactional(TransactionMode.COMMIT) before my test . That was the dumb solution.
Related
I try select data from the table by a filter with Spring Data JPA Specification I think what my implementation is correct, But it doesn't work. Help me please understand my mistake and fix my example.
I have very strange SQL query in log :
select phone0_.id as id1_0_, phone0_.note as note2_0_, phone0_.number as number3_0_, phone0_.operator_login as operator4_0_, phone0_.operator_pass as operator5_0_, phone0_.operator_name as operator6_0_, phone0_.operator_url as operator7_0_, phone0_.reg_date as reg_date8_0_, phone0_.status as status9_0_ from phone phone0_ where 0=1 limit ?
In the end: where 0=1 it's crash my mind. Where did that come from?
Here I fill CriteriaBuilder if filter field not null. I expect to get correctly built Specification object and send it to findAll(Specifications.where(specification), Pageable p) method. But something incorrect.
My repo and specification impl:
public interface PhoneRepository extends CrudRepository<Phone, Integer>, JpaRepository<Phone, Integer>, JpaSpecificationExecutor<Phone> {
class PhoneSpecification implements Specification<Phone> {
private final #NonNull PhoneService.PhoneFilter filter;
public PhoneSpecification(#NonNull PhoneService.PhoneFilter filter) {
this.filter = filter;
}
#Override
public Predicate toPredicate(Root<Phone> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Predicate predicate = cb.disjunction();
if (nonNull(filter.getId())) {
cb.disjunction().getExpressions().add(cb.equal(root.get("id"), filter.getId()));
}
if (nonNull(filter.getNote())) {
cb.disjunction().getExpressions().add(cb.like(root.get("note"), filter.getNote()));
}
if (nonNull(filter.getNumber())) {
cb.disjunction().getExpressions().add(cb.like(root.get("number"), filter.getNumber()));
}
if (nonNull(filter.getStatus())) {
cb.disjunction().getExpressions().add(cb.like(root.get("status"), filter.getStatus()));
}
if (nonNull(filter.getOpName())) {
cb.disjunction().getExpressions().add(cb.like(root.get("operatorName"), filter.getOpName()));
}
if (nonNull(filter.getOpLogin())) {
cb.disjunction().getExpressions().add(cb.like(root.get("operatorAccLogin"), filter.getOpLogin()));
}
if (nonNull(filter.getOpPassword())) {
cb.disjunction().getExpressions().add(cb.like(root.get("operatorAccPassword"), filter.getOpPassword()));
}
if (nonNull(filter.getRegFrom()) && nonNull(filter.getRegTo())) {
cb.disjunction().getExpressions().add(cb.between(root.get("regDate"), filter.getRegFrom(), filter.getRegTo()));
}
return predicate;
}
}
}
This is service level:
#Service
public class PhoneService {
#Autowired
private PhoneRepository phoneRepository;
public Phone get(int id) {
Phone phone = phoneRepository.findOne(id);
return nonNull(phone) ? phone : new Phone();
}
public Page<Phone> list(#NonNull PhoneFilter filter) {
PhoneSpecification specification = new PhoneSpecification(filter);
return phoneRepository.findAll(Specifications.where(specification), filter.getPageable());
}
#Data
public static class PhoneFilter {
private Pageable pageable;
private Integer id;
private Timestamp regFrom;
private Timestamp regTo;
private String number;
private String opLogin;
private String opPassword;
private String opName;
private String status;
private String note;
}
}
And entity
#Entity
#Data
#NoArgsConstructor
#AllArgsConstructor
#Table(name = "phone")
#ToString(exclude = {"accounts"})
#EqualsAndHashCode(exclude = {"accounts"})
public class Phone {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
#OneToMany(mappedBy = "phone", cascade = CascadeType.DETACH)
private Collection<SocialAccount> accounts;
#Column(name = "reg_date")
private Timestamp regDate;
#Column(name = "number")
private String number;
#Column(name = "operator_url")
private String operatorUrl;
#Column(name = "operator_login")
private String operatorAccLogin;
#Column(name = "operator_pass")
private String operatorAccPassword;
#Column(name = "operator_name")
private String operatorName;
#Column(name = "status")
private String status;
#Column(name = "note")
private String note;
}
I find the mistake.
Method CriteriaBuilder.disjunction() this is factory and each time when I call him I got new Predicate object.
This implementation CriteriaBuilderImpl:
public Predicate disjunction() {
return new CompoundPredicate(this, BooleanOperator.OR);
}
Be careful with it.
I've finding a solution but nothing works for me, here's the code:
Update function:
#Autowired
private SessionFactory sessionFactory;
...
public void updatePositionProfile(PositionProfile positionProfile) {
Session session = sessionFactory.getCurrentSession();
session.merge(positionProfile);
session.flush();
}
Entity (getters and setter ommited):
#Entity
#Table(name = "position_profile")
public class PositionProfile implements Serializable {
#Embeddable
public static class PositionProfile_PK implements Serializable {
private static final long serialVersionUID = 1L;
#NotNull
#Column(name="id_position")
Integer id_position;
#NotNull
#Column(name="profile")
String profile;
#NotNull
#Column(name="line")
String line;
PositionProfile_PK(){
this.id_position = 0;
this.profile = new String();
this.line = "";
}
}
#Id
PositionProfile_PK positionProfilePK;
#NotNull
#Column(name="MAX_SPEED")
private Integer max_speed;
#NotNull
#Column(name="WARNING_SPEED")
private Integer warning_speed;
#NotNull
#Column(name="EMERGENCY_SPEED")
private Integer emergency_speed;
#NotNull
#Column(name="DISABLED")
private String disabled;
PositionProfile(){
super();
this.positionProfilePK = new PositionProfile_PK();
this.max_speed = 0;
this.warning_speed = 0;
this.emergency_speed = 0;
this.disabled = " ";
}
}
Controller (summarized for brevity):
PositionProfile positionProfileToUpdate = positionProfile.getPositionProfileByIdPositionAndProfile(pk, profile);
positionProfileToUpdate.setMax_speed(ms);
positionProfile.updatePositionProfile(positionProfileToUpdate);
I've tryed with update() function and saveOrUpdate() but it doesn't work, I don't know what's happening. Session is never closed so the entity is attached. I've checked that values are changed correctly in the object I passed to updatePositionProfile() function, but when merge() it simply does nothing.
Thanks!
if you make flush, the PositionProfile is only for the same session visible.
You must check, if the sessionFactory.getCurrentSession(); working correctly
I am using the GAE Datastore for the application
I am in despair ... I hit a mental block over here and I just cant think of anything to solve this anymore. I have a class Teacher.class (and all of its CASCADES) that wont store in its Namespace ... all other classes (not shown here, but very similar) work like a charm, the Teacher.class reads and writes and all perfectly ... except if WONT go to its Namespace, it always ends up in the Empty Namespace.
I am going to post the class, along with the data interface layer.
#Entity
public class Teacher implements Serializable
{
private static final long serialVersionUID = 5426530769458891752L;
#Id
private Key key;
private long KID;
private long school;
private String FName;
private String LName;
private String Email;
private String SchoolName;
#OneToOne(cascade = CascadeType.ALL,fetch=FetchType.LAZY)
private Transcript Transcript; // Contains further #OneToMany Relations and Constructors
#OneToOne(cascade = CascadeType.ALL,fetch=FetchType.LAZY)
private TeacherInfo teacherInf; // Contains Only Primitive Type Objects(not important)
private Boolean ActiveUser = false;
private List<Key> WorkshopsAttended;
private List<Key> WorkshopsRegistered;
public Teacher()//Constructor
{
if(this.KID == 00)
{
this.KID = TeacherUtils.genKID();//Returns a sequence and date and location based long
this.key = KeyFactory.createKey(Teacher.class.getSimpleName(), this.KID);
this.Transcript = new Transcript();
this.teacherInf = new TeacherInfo();
}
if(this.WorkshopsAttended == null)
{
this.WorkshopsAttended = new ArrayList<Key>();
}
if(this.WorkshopsRegistered == null)
{
this.WorkshopsRegistered = new ArrayList<Key>();
}
} //End of Constructor
//Getters and Setters
}
Here is the Transcript Class
#Entity
public class Transcript implements Serializable
{
private static final long serialVersionUID = -6677626465437896027L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Key ID;
#OneToOne(cascade = CascadeType.ALL, mappedBy="Transcript",fetch=FetchType.LAZY)
private Teacher teacher;
public Transcript()//Constructor
{
if(this.C1== null)
{this.C1= new Course1();}
if (this.C2== null)
{this.C2= new Course2();}
if (this.C3== null)
{this.C3= new Course3();}
}//End of Constructor
#OneToOne(cascade = CascadeType.ALL,fetch=FetchType.EAGER)
private Course1 C1; // Contains only primitive feilds
#OneToOne(cascade = CascadeType.ALL,fetch=FetchType.EAGER)
private Course2 C2; // Contains only primitive feilds
#OneToOne(cascade = CascadeType.ALL,fetch=FetchType.EAGER)
private Course3 C3;
// Getters and Setters
Now Lastly the Data Interface Layer (This is a very long long file and I cant put it all in here, so I am just gonna paste the bits that have to do with persisting Teacher entities)
public class TeacherUtils
{
private static final String ActiveNamespace = SystemSettings.TeacherActive;
private static final String DeletedNamespace = SystemSettings.TeacherDeleted;
private static final boolean NSFlag = (SystemSettings.UseNameSpace & SystemSettings.TeacherNameSpace);
public synchronized static void SaveTeacher(Teacher teacher)
{
EntityManager em = getActiveEM();
em.persist(teacher);
em.flush();
closeEM(em);
}// End of SaveTeacher
public synchronized static void UpdateTeacher(Teacher teacher)
{
EntityManager em = getActiveEM();
em.merge(teacher);
em.flush();
closeEM(em);
}// End of Update Teacher
private synchronized static EntityManager getActiveEM()
{
EntityManager em;
try
{
if( (!NamespaceManager.get().equals(ActiveNamespace)) && TeacherUtils.NSFlag)
{
setNamespace();
}
}
finally
{
em = EMF.get().createEntityManager();
em.getTransaction().begin();
}
return em;
} // End of getAciveEM();
private synchronized static void closeEM(EntityManager em)
{
em.getTransaction().commit();
em.close();
NamespaceManager.set("");
while(!NamespaceManager.get().equals(""))
{}
return;
}// End of CloseEM(em)
private synchronized static void setNamespace()
{
if(TeacherUtils.NSFlag)
{
NamespaceManager.set(TeacherUtils.ActiveNamespace);
while(!NamespaceManager.get().equals(TeacherUtils.ActiveNamespace))
{}
}
}// End of setNamespace
A typical example in the Business Logic Layer would be
Teacher teacher = new Teacher();
teacher.setFName("John");
teacher.setLName("Smith");
teacher.setEmail("xyz#xyz.com");
TeacherUtils.SaveTeacher(teacher);
I would like to thank everyone who tried looking into this question ! Turns out there was something wrong with the Teacher.class constructor logic that caused a change in the Namespace, without reverting it to its previous state (the ID generator function). Now that I took care of it all works great !! I think it's just one of those days !!
Again thanks alot and I will be leaving this code as a template incase someone needs it.
I have a CrudRepository that is supposed to make a query with an array (findByIn). In my repository tests it works, but when I try to use the query in my service, it doesn't work. Could someone explain why it doesn't work? Here is my setup (excluding some code irrelevant to the question)
Database model:
#Entity
#Table(name="Place")
public class Place implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "placeId", nullable = false)
private Long placeId;
#Column(name = "owner", nullable = false)
private String owner;
public Long getPlaceId() {
return placeId;
}
public void setPlaceId(Long placeId) {
this.placeId = placeId;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
}
Repository:
#Repository
public interface PlaceRepository extends CrudRepository<Place, Long> {
List<Place> findByPlaceIdIn(Long[] placeId);
}
Service (this is the part not working):
#Service
public class PlaceService {
#Autowired
private PlaceRepository placeRepository;
public List<Place> getPlaces(Long[] placeIds) {
return placeRepository.findByPlaceIdIn(placeIds);
}
}
The problem is that in my service placeRepository.findByPlaceIdIn(placeIds) returns 0 objects if placeIds contains more than one item. If placeIds contains just one item, the query works fine. I tried replacing return placeRepository.findByPlaceIdIn(placeIds) with this piece of code that does the query for every array item one by one (this actually works, but I'd like to get the query work as it should):
ArrayList<Place> places = new ArrayList<Place>();
for (Long placeId : placeIds) {
Long[] id = {placeId};
places.addAll(placeRepository.findByPlaceIdIn(id));
}
return places;
I know that the repository should work, because I have a working test for it:
public class PlaceRepositoryTest {
#Autowired
private PlaceRepository repository;
private static Place place;
private static Place place2;
private static Place otherUsersPlace;
#Test
public void testPlacesfindByPlaceIdIn() {
place = new Place();
place.setOwner(USER_ID);
place2 = new Place();
place2.setOwner(USER_ID);
place = repository.save(place);
place2 = repository.save(place2);
Long[] ids = {place.getPlaceId(), place2.getPlaceId()};
assertEquals(repository.findByPlaceIdIn(ids).size(), 2);
}
}
I also have another repository for other model, which also uses findByIn and it works fine. I can't see any relevant difference between the repositories. I thought it might offer some more details to show the working repository, so I included it below:
Database model:
#Entity
#Table(name="LocalDatabaseRow")
#JsonIgnoreProperties(ignoreUnknown=false)
public class LocalDatabaseRow implements Serializable {
public LocalDatabaseRow() {}
public LocalDatabaseRow(RowType rowType) {
this.rowType = rowType;
}
public enum RowType {
TYPE1,
TYPE2
};
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "id")
#JsonProperty("id")
private Long id;
#JsonProperty("rowType")
#Column(name = "rowType")
private RowType rowType;
public Long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public RowType getRowType() {
return rowType;
}
public void setRowType(RowType rowType) {
this.rowType = rowType;
}
}
Repository:
#Repository
public interface LocalDatabaseRowRepository extends CrudRepository<LocalDatabaseRow, Long> {
List<LocalDatabaseRow> findByRowTypeAndUserIdIn(RowType type, String[] userId);
}
try using a list instead :
findByPlaceIdIn(List placeIdList);
You have a typo in your code (the repository declaration in the service):
#Autowired
private placeRepository placeRepository;
Should be:
#Autowired
private PlaceRepository placeRepository;
I have this Play Model class that I'm trying to modify an object of, and when I want to save it, I get the following exception:
java.lang.RuntimeException: No #javax.persistence.Id field found in class [class models.Contact]
at play.db.ebean.Model._idAccessors(Model.java:39)
at play.db.ebean.Model._getId(Model.java:52)
The class:
#Entity
public class Contact extends Model implements Person {//, Comparable<Contact>{
private Long id;
private Client client;
#Required
private String email;
private String profil_picture;
private Boolean active = new Boolean(true);
private Boolean favorite = new Boolean(false);
#Transient
private Boolean profile_pic_url_init = new Boolean(false);
#Id
#GeneratedValue
public Long getId() {
return id;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name="client_id")
public Client getClient(){
return client;
}
public void setClient(Client client){
this.client= client;
}
#Column
public Boolean getFavorite() {
return favorite;
}
public void setFavorite(Boolean is_favorite) {
this.favorite = is_favorite;
}
....
}
The code calling the save() method:
List<Contact> contacts_list = current_client.getContacts();
for (Contact c : contacts_list) {
c.setFavorite(false);
c.save();
}
The class actually has an #Id annotation, so any guesses of why this doesn't work? I tried looking it up on google, but couldn't find much about this error. Thanks in advance!
Move #Id annotation to id field instead of its getter.