Saving integer array using Spring Data JPA












0















I am using spring data jpa with hibernate and postgres. I am trying to save an integer array in a column. I am using vlad mihaceas library for persisting the array into postgresql. The entity is as follows:-



@Type(type = "int-array")
@Column(name = "location", columnDefinition = "integer")
private Integer locations;


The corresponding location entity is



@Entity
@Table(name = "location_master")
public class Location implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "location_name")
private String locationName;
}


The location ids are being saved in the db. But I am not able to display the in thymeleaf.



<tr>
<td>Selected Locations</td>
<td>[[${office.locations[0].locationName}]]</td>
</tr>


The following error has come up:-




org.springframework.expression.spel.SpelEvaluationException: EL1008E:
Property or field 'locationName' cannot be found on object of type
'java.lang.Integer' - maybe not public or not valid?











share|improve this question




















  • 1





    Well, the message says it all. Why do you think you can get a locationName out of an Integer? That makes no sense. An Integer is an Integer. Not a Location. If you want a Collection<Location>, then use a Collection<Location>, and make it a OneToMany association between your unknown entity and the Location entity.

    – JB Nizet
    Nov 15 '18 at 7:49













  • @JB Nizet I want to save the selected location ids in the database as array. I already tried with List<Integer> which gave the error Use of OneToMany or ManyToMany targeting an unmapped class. As Hibernate maps only primitive types, i was using vlad mihalcea's library

    – John
    Nov 15 '18 at 8:46











  • That's not proper database design. Use foreign keys, join columns, join tables, and associations between entities.

    – JB Nizet
    Nov 15 '18 at 9:36











  • @JBNizet Database design is not in my control. I have to work with what is given to me.

    – John
    Nov 15 '18 at 9:51











  • Try to use @ElementCollection and @CollectionTable annotations

    – Nick Savenia
    Nov 15 '18 at 9:55
















0















I am using spring data jpa with hibernate and postgres. I am trying to save an integer array in a column. I am using vlad mihaceas library for persisting the array into postgresql. The entity is as follows:-



@Type(type = "int-array")
@Column(name = "location", columnDefinition = "integer")
private Integer locations;


The corresponding location entity is



@Entity
@Table(name = "location_master")
public class Location implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "location_name")
private String locationName;
}


The location ids are being saved in the db. But I am not able to display the in thymeleaf.



<tr>
<td>Selected Locations</td>
<td>[[${office.locations[0].locationName}]]</td>
</tr>


The following error has come up:-




org.springframework.expression.spel.SpelEvaluationException: EL1008E:
Property or field 'locationName' cannot be found on object of type
'java.lang.Integer' - maybe not public or not valid?











share|improve this question




















  • 1





    Well, the message says it all. Why do you think you can get a locationName out of an Integer? That makes no sense. An Integer is an Integer. Not a Location. If you want a Collection<Location>, then use a Collection<Location>, and make it a OneToMany association between your unknown entity and the Location entity.

    – JB Nizet
    Nov 15 '18 at 7:49













  • @JB Nizet I want to save the selected location ids in the database as array. I already tried with List<Integer> which gave the error Use of OneToMany or ManyToMany targeting an unmapped class. As Hibernate maps only primitive types, i was using vlad mihalcea's library

    – John
    Nov 15 '18 at 8:46











  • That's not proper database design. Use foreign keys, join columns, join tables, and associations between entities.

    – JB Nizet
    Nov 15 '18 at 9:36











  • @JBNizet Database design is not in my control. I have to work with what is given to me.

    – John
    Nov 15 '18 at 9:51











  • Try to use @ElementCollection and @CollectionTable annotations

    – Nick Savenia
    Nov 15 '18 at 9:55














0












0








0








I am using spring data jpa with hibernate and postgres. I am trying to save an integer array in a column. I am using vlad mihaceas library for persisting the array into postgresql. The entity is as follows:-



@Type(type = "int-array")
@Column(name = "location", columnDefinition = "integer")
private Integer locations;


The corresponding location entity is



@Entity
@Table(name = "location_master")
public class Location implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "location_name")
private String locationName;
}


The location ids are being saved in the db. But I am not able to display the in thymeleaf.



<tr>
<td>Selected Locations</td>
<td>[[${office.locations[0].locationName}]]</td>
</tr>


The following error has come up:-




org.springframework.expression.spel.SpelEvaluationException: EL1008E:
Property or field 'locationName' cannot be found on object of type
'java.lang.Integer' - maybe not public or not valid?











share|improve this question
















I am using spring data jpa with hibernate and postgres. I am trying to save an integer array in a column. I am using vlad mihaceas library for persisting the array into postgresql. The entity is as follows:-



@Type(type = "int-array")
@Column(name = "location", columnDefinition = "integer")
private Integer locations;


The corresponding location entity is



@Entity
@Table(name = "location_master")
public class Location implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "location_name")
private String locationName;
}


The location ids are being saved in the db. But I am not able to display the in thymeleaf.



<tr>
<td>Selected Locations</td>
<td>[[${office.locations[0].locationName}]]</td>
</tr>


The following error has come up:-




org.springframework.expression.spel.SpelEvaluationException: EL1008E:
Property or field 'locationName' cannot be found on object of type
'java.lang.Integer' - maybe not public or not valid?








java spring postgresql hibernate spring-data-jpa






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 6:57









Vlad Mihalcea

56.8k13153452




56.8k13153452










asked Nov 15 '18 at 7:44









JohnJohn

1541312




1541312








  • 1





    Well, the message says it all. Why do you think you can get a locationName out of an Integer? That makes no sense. An Integer is an Integer. Not a Location. If you want a Collection<Location>, then use a Collection<Location>, and make it a OneToMany association between your unknown entity and the Location entity.

    – JB Nizet
    Nov 15 '18 at 7:49













  • @JB Nizet I want to save the selected location ids in the database as array. I already tried with List<Integer> which gave the error Use of OneToMany or ManyToMany targeting an unmapped class. As Hibernate maps only primitive types, i was using vlad mihalcea's library

    – John
    Nov 15 '18 at 8:46











  • That's not proper database design. Use foreign keys, join columns, join tables, and associations between entities.

    – JB Nizet
    Nov 15 '18 at 9:36











  • @JBNizet Database design is not in my control. I have to work with what is given to me.

    – John
    Nov 15 '18 at 9:51











  • Try to use @ElementCollection and @CollectionTable annotations

    – Nick Savenia
    Nov 15 '18 at 9:55














  • 1





    Well, the message says it all. Why do you think you can get a locationName out of an Integer? That makes no sense. An Integer is an Integer. Not a Location. If you want a Collection<Location>, then use a Collection<Location>, and make it a OneToMany association between your unknown entity and the Location entity.

    – JB Nizet
    Nov 15 '18 at 7:49













  • @JB Nizet I want to save the selected location ids in the database as array. I already tried with List<Integer> which gave the error Use of OneToMany or ManyToMany targeting an unmapped class. As Hibernate maps only primitive types, i was using vlad mihalcea's library

    – John
    Nov 15 '18 at 8:46











  • That's not proper database design. Use foreign keys, join columns, join tables, and associations between entities.

    – JB Nizet
    Nov 15 '18 at 9:36











  • @JBNizet Database design is not in my control. I have to work with what is given to me.

    – John
    Nov 15 '18 at 9:51











  • Try to use @ElementCollection and @CollectionTable annotations

    – Nick Savenia
    Nov 15 '18 at 9:55








1




1





Well, the message says it all. Why do you think you can get a locationName out of an Integer? That makes no sense. An Integer is an Integer. Not a Location. If you want a Collection<Location>, then use a Collection<Location>, and make it a OneToMany association between your unknown entity and the Location entity.

– JB Nizet
Nov 15 '18 at 7:49







Well, the message says it all. Why do you think you can get a locationName out of an Integer? That makes no sense. An Integer is an Integer. Not a Location. If you want a Collection<Location>, then use a Collection<Location>, and make it a OneToMany association between your unknown entity and the Location entity.

– JB Nizet
Nov 15 '18 at 7:49















@JB Nizet I want to save the selected location ids in the database as array. I already tried with List<Integer> which gave the error Use of OneToMany or ManyToMany targeting an unmapped class. As Hibernate maps only primitive types, i was using vlad mihalcea's library

– John
Nov 15 '18 at 8:46





@JB Nizet I want to save the selected location ids in the database as array. I already tried with List<Integer> which gave the error Use of OneToMany or ManyToMany targeting an unmapped class. As Hibernate maps only primitive types, i was using vlad mihalcea's library

– John
Nov 15 '18 at 8:46













That's not proper database design. Use foreign keys, join columns, join tables, and associations between entities.

– JB Nizet
Nov 15 '18 at 9:36





That's not proper database design. Use foreign keys, join columns, join tables, and associations between entities.

– JB Nizet
Nov 15 '18 at 9:36













@JBNizet Database design is not in my control. I have to work with what is given to me.

– John
Nov 15 '18 at 9:51





@JBNizet Database design is not in my control. I have to work with what is given to me.

– John
Nov 15 '18 at 9:51













Try to use @ElementCollection and @CollectionTable annotations

– Nick Savenia
Nov 15 '18 at 9:55





Try to use @ElementCollection and @CollectionTable annotations

– Nick Savenia
Nov 15 '18 at 9:55












1 Answer
1






active

oldest

votes


















0















org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'locationName' cannot be found on object of type 'java.lang.Integer' - maybe not public or not valid?




The error has nothing to do with the use of the int-array of the hibernate-types project.



The error message is about the Spring Expression Language you used for setting the locationName
which is a String property.



[[${office.locations[0].locationName}]]


The locations property is an Integer, but you treated it as a Location array.



What you want is a @OneToMany List<Location> instead:



@OneToMany(mappedBy="office", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Location> locations = new ArrayList<>();


And a @ManyToOne association in Location:



@ManyToOne(fetch = FetchType.LAZY)
private Office office;


Check out this article for more details about the best way to use a @OneToMany association.






share|improve this answer
























  • I know that it is a SpEL error and nothing to do with hibernate-types. Perhaps i could not explain my problem properly. The OneToMany List<Location mapping is also fine with me. My question is whether the location ids will be stored in the database as an integer array by this association.

    – John
    Nov 24 '18 at 7:26











  • Of course, they will. Check out the hibernate-types tests in the GitHub repository for a proof.

    – Vlad Mihalcea
    Nov 24 '18 at 8:09











  • I don't need proof. Hibernate types is already working fine for me in jsonb types. I am only confused as how to use @Type type= int-array annotation in these mappings. You also didn't mention it in your answer.

    – John
    Nov 24 '18 at 9:04











  • I would have mentioned it the exception message would indicate that. Check out this test case to see how to use the int-array mapping. Just compare my test case with yours and see where they differ.

    – Vlad Mihalcea
    Nov 24 '18 at 11:44











  • Yes, i have seen it. You have an array of integers and in my case, it is a list of objects. I am not able to figure out how to do the int-array mapping for this list

    – John
    Nov 24 '18 at 11:58











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314572%2fsaving-integer-array-using-spring-data-jpa%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0















org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'locationName' cannot be found on object of type 'java.lang.Integer' - maybe not public or not valid?




The error has nothing to do with the use of the int-array of the hibernate-types project.



The error message is about the Spring Expression Language you used for setting the locationName
which is a String property.



[[${office.locations[0].locationName}]]


The locations property is an Integer, but you treated it as a Location array.



What you want is a @OneToMany List<Location> instead:



@OneToMany(mappedBy="office", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Location> locations = new ArrayList<>();


And a @ManyToOne association in Location:



@ManyToOne(fetch = FetchType.LAZY)
private Office office;


Check out this article for more details about the best way to use a @OneToMany association.






share|improve this answer
























  • I know that it is a SpEL error and nothing to do with hibernate-types. Perhaps i could not explain my problem properly. The OneToMany List<Location mapping is also fine with me. My question is whether the location ids will be stored in the database as an integer array by this association.

    – John
    Nov 24 '18 at 7:26











  • Of course, they will. Check out the hibernate-types tests in the GitHub repository for a proof.

    – Vlad Mihalcea
    Nov 24 '18 at 8:09











  • I don't need proof. Hibernate types is already working fine for me in jsonb types. I am only confused as how to use @Type type= int-array annotation in these mappings. You also didn't mention it in your answer.

    – John
    Nov 24 '18 at 9:04











  • I would have mentioned it the exception message would indicate that. Check out this test case to see how to use the int-array mapping. Just compare my test case with yours and see where they differ.

    – Vlad Mihalcea
    Nov 24 '18 at 11:44











  • Yes, i have seen it. You have an array of integers and in my case, it is a list of objects. I am not able to figure out how to do the int-array mapping for this list

    – John
    Nov 24 '18 at 11:58
















0















org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'locationName' cannot be found on object of type 'java.lang.Integer' - maybe not public or not valid?




The error has nothing to do with the use of the int-array of the hibernate-types project.



The error message is about the Spring Expression Language you used for setting the locationName
which is a String property.



[[${office.locations[0].locationName}]]


The locations property is an Integer, but you treated it as a Location array.



What you want is a @OneToMany List<Location> instead:



@OneToMany(mappedBy="office", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Location> locations = new ArrayList<>();


And a @ManyToOne association in Location:



@ManyToOne(fetch = FetchType.LAZY)
private Office office;


Check out this article for more details about the best way to use a @OneToMany association.






share|improve this answer
























  • I know that it is a SpEL error and nothing to do with hibernate-types. Perhaps i could not explain my problem properly. The OneToMany List<Location mapping is also fine with me. My question is whether the location ids will be stored in the database as an integer array by this association.

    – John
    Nov 24 '18 at 7:26











  • Of course, they will. Check out the hibernate-types tests in the GitHub repository for a proof.

    – Vlad Mihalcea
    Nov 24 '18 at 8:09











  • I don't need proof. Hibernate types is already working fine for me in jsonb types. I am only confused as how to use @Type type= int-array annotation in these mappings. You also didn't mention it in your answer.

    – John
    Nov 24 '18 at 9:04











  • I would have mentioned it the exception message would indicate that. Check out this test case to see how to use the int-array mapping. Just compare my test case with yours and see where they differ.

    – Vlad Mihalcea
    Nov 24 '18 at 11:44











  • Yes, i have seen it. You have an array of integers and in my case, it is a list of objects. I am not able to figure out how to do the int-array mapping for this list

    – John
    Nov 24 '18 at 11:58














0












0








0








org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'locationName' cannot be found on object of type 'java.lang.Integer' - maybe not public or not valid?




The error has nothing to do with the use of the int-array of the hibernate-types project.



The error message is about the Spring Expression Language you used for setting the locationName
which is a String property.



[[${office.locations[0].locationName}]]


The locations property is an Integer, but you treated it as a Location array.



What you want is a @OneToMany List<Location> instead:



@OneToMany(mappedBy="office", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Location> locations = new ArrayList<>();


And a @ManyToOne association in Location:



@ManyToOne(fetch = FetchType.LAZY)
private Office office;


Check out this article for more details about the best way to use a @OneToMany association.






share|improve this answer














org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'locationName' cannot be found on object of type 'java.lang.Integer' - maybe not public or not valid?




The error has nothing to do with the use of the int-array of the hibernate-types project.



The error message is about the Spring Expression Language you used for setting the locationName
which is a String property.



[[${office.locations[0].locationName}]]


The locations property is an Integer, but you treated it as a Location array.



What you want is a @OneToMany List<Location> instead:



@OneToMany(mappedBy="office", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Location> locations = new ArrayList<>();


And a @ManyToOne association in Location:



@ManyToOne(fetch = FetchType.LAZY)
private Office office;


Check out this article for more details about the best way to use a @OneToMany association.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 6:57









Vlad MihalceaVlad Mihalcea

56.8k13153452




56.8k13153452













  • I know that it is a SpEL error and nothing to do with hibernate-types. Perhaps i could not explain my problem properly. The OneToMany List<Location mapping is also fine with me. My question is whether the location ids will be stored in the database as an integer array by this association.

    – John
    Nov 24 '18 at 7:26











  • Of course, they will. Check out the hibernate-types tests in the GitHub repository for a proof.

    – Vlad Mihalcea
    Nov 24 '18 at 8:09











  • I don't need proof. Hibernate types is already working fine for me in jsonb types. I am only confused as how to use @Type type= int-array annotation in these mappings. You also didn't mention it in your answer.

    – John
    Nov 24 '18 at 9:04











  • I would have mentioned it the exception message would indicate that. Check out this test case to see how to use the int-array mapping. Just compare my test case with yours and see where they differ.

    – Vlad Mihalcea
    Nov 24 '18 at 11:44











  • Yes, i have seen it. You have an array of integers and in my case, it is a list of objects. I am not able to figure out how to do the int-array mapping for this list

    – John
    Nov 24 '18 at 11:58



















  • I know that it is a SpEL error and nothing to do with hibernate-types. Perhaps i could not explain my problem properly. The OneToMany List<Location mapping is also fine with me. My question is whether the location ids will be stored in the database as an integer array by this association.

    – John
    Nov 24 '18 at 7:26











  • Of course, they will. Check out the hibernate-types tests in the GitHub repository for a proof.

    – Vlad Mihalcea
    Nov 24 '18 at 8:09











  • I don't need proof. Hibernate types is already working fine for me in jsonb types. I am only confused as how to use @Type type= int-array annotation in these mappings. You also didn't mention it in your answer.

    – John
    Nov 24 '18 at 9:04











  • I would have mentioned it the exception message would indicate that. Check out this test case to see how to use the int-array mapping. Just compare my test case with yours and see where they differ.

    – Vlad Mihalcea
    Nov 24 '18 at 11:44











  • Yes, i have seen it. You have an array of integers and in my case, it is a list of objects. I am not able to figure out how to do the int-array mapping for this list

    – John
    Nov 24 '18 at 11:58

















I know that it is a SpEL error and nothing to do with hibernate-types. Perhaps i could not explain my problem properly. The OneToMany List<Location mapping is also fine with me. My question is whether the location ids will be stored in the database as an integer array by this association.

– John
Nov 24 '18 at 7:26





I know that it is a SpEL error and nothing to do with hibernate-types. Perhaps i could not explain my problem properly. The OneToMany List<Location mapping is also fine with me. My question is whether the location ids will be stored in the database as an integer array by this association.

– John
Nov 24 '18 at 7:26













Of course, they will. Check out the hibernate-types tests in the GitHub repository for a proof.

– Vlad Mihalcea
Nov 24 '18 at 8:09





Of course, they will. Check out the hibernate-types tests in the GitHub repository for a proof.

– Vlad Mihalcea
Nov 24 '18 at 8:09













I don't need proof. Hibernate types is already working fine for me in jsonb types. I am only confused as how to use @Type type= int-array annotation in these mappings. You also didn't mention it in your answer.

– John
Nov 24 '18 at 9:04





I don't need proof. Hibernate types is already working fine for me in jsonb types. I am only confused as how to use @Type type= int-array annotation in these mappings. You also didn't mention it in your answer.

– John
Nov 24 '18 at 9:04













I would have mentioned it the exception message would indicate that. Check out this test case to see how to use the int-array mapping. Just compare my test case with yours and see where they differ.

– Vlad Mihalcea
Nov 24 '18 at 11:44





I would have mentioned it the exception message would indicate that. Check out this test case to see how to use the int-array mapping. Just compare my test case with yours and see where they differ.

– Vlad Mihalcea
Nov 24 '18 at 11:44













Yes, i have seen it. You have an array of integers and in my case, it is a list of objects. I am not able to figure out how to do the int-array mapping for this list

– John
Nov 24 '18 at 11:58





Yes, i have seen it. You have an array of integers and in my case, it is a list of objects. I am not able to figure out how to do the int-array mapping for this list

– John
Nov 24 '18 at 11:58


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314572%2fsaving-integer-array-using-spring-data-jpa%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()