Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7278489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:52:53+00:00 2026-05-28T22:52:53+00:00

I’m trying to run my first example with spring. So I downloaded SpringSource Tool

  • 0

I’m trying to run my first example with spring. So I downloaded SpringSource Tool Suite and I created a jpa project from template.

I have a mysql database where I created a User table. So I created the user class:

import java.io.Serializable;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;

import java.util.Date;
import java.util.Set;


/**
 * The persistent class for the user database table.
 * 
 */
@XmlRootElement(name="User")
@Entity
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private String username;

    private String cellphone;

    private int currentlat;

    private int currentlon;

    private String email;

    @Lob()
    private byte[] foto;

    @Temporal( TemporalType.TIMESTAMP)
    private Date lastgpsdate;

    private String name;

    private String notes;

    private String password;

    private String surname;

    //bi-directional many-to-one association to FavoriteSport
    @OneToMany(mappedBy="user")
    private Set<FavoriteSport> favoriteSports;

    //bi-directional many-to-one association to FavoriteSpot
    @OneToMany(mappedBy="user")
    private Set<FavoriteSpot> favoriteSpots;

    //bi-directional many-to-one association to FavoriteUser
    @OneToMany(mappedBy="user1")
    private Set<FavoriteUser> favoriteUsers1;

    //bi-directional many-to-one association to FavoriteUser
    @OneToMany(mappedBy="user2")
    private Set<FavoriteUser> favoriteUsers2;

    //bi-directional many-to-one association to SpotReview
    @OneToMany(mappedBy="user")
    private Set<SpotReview> spotReviews;

    //bi-directional many-to-one association to Role
    @ManyToOne
    @JoinColumn(name="role_idrole")
    private Role role;

    //bi-directional many-to-one association to UserAccount
    @OneToMany(mappedBy="user")
    private Set<UserAccount> userAccounts;

    public User() {
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getCellphone() {
        return this.cellphone;
    }

    public void setCellphone(String cellphone) {
        this.cellphone = cellphone;
    }

    public int getCurrentlat() {
        return this.currentlat;
    }

    public void setCurrentlat(int currentlat) {
        this.currentlat = currentlat;
    }

    public int getCurrentlon() {
        return this.currentlon;
    }

    public void setCurrentlon(int currentlon) {
        this.currentlon = currentlon;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public byte[] getFoto() {
        return this.foto;
    }

    public void setFoto(byte[] foto) {
        this.foto = foto;
    }

    public Date getLastgpsdate() {
        return this.lastgpsdate;
    }

    public void setLastgpsdate(Date lastgpsdate) {
        this.lastgpsdate = lastgpsdate;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNotes() {
        return this.notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSurname() {
        return this.surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public Set<FavoriteSport> getFavoriteSports() {
        return this.favoriteSports;
    }

    public void setFavoriteSports(Set<FavoriteSport> favoriteSports) {
        this.favoriteSports = favoriteSports;
    }

    public Set<FavoriteSpot> getFavoriteSpots() {
        return this.favoriteSpots;
    }

    public void setFavoriteSpots(Set<FavoriteSpot> favoriteSpots) {
        this.favoriteSpots = favoriteSpots;
    }

    public Set<FavoriteUser> getFavoriteUsers1() {
        return this.favoriteUsers1;
    }

    public void setFavoriteUsers1(Set<FavoriteUser> favoriteUsers1) {
        this.favoriteUsers1 = favoriteUsers1;
    }

    public Set<FavoriteUser> getFavoriteUsers2() {
        return this.favoriteUsers2;
    }

    public void setFavoriteUsers2(Set<FavoriteUser> favoriteUsers2) {
        this.favoriteUsers2 = favoriteUsers2;
    }

    public Set<SpotReview> getSpotReviews() {
        return this.spotReviews;
    }

    public void setSpotReviews(Set<SpotReview> spotReviews) {
        this.spotReviews = spotReviews;
    }

    public Role getRole() {
        return this.role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public Set<UserAccount> getUserAccounts() {
        return this.userAccounts;
    }

    public void setUserAccounts(Set<UserAccount> userAccounts) {
        this.userAccounts = userAccounts;
    }

}

Then I defined the Dao structure and the implementation of the JpaDaoUser

public interface Dao <E, K> {
    void persist(E entity);
    void remove(E entity);
    E findById(K id);
}

import java.lang.reflect.ParameterizedType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public abstract class JpaDao <E, K> implements Dao<E, K> {
    protected Class<E> entityClass;

    @PersistenceContext
    protected EntityManager entityManager;

    public JpaDao() {
        ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
        this.entityClass = (Class<E>) genericSuperclass.getActualTypeArguments()[1];
    }

    public void persist(E entity) { entityManager.persist(entity); }

    public void remove(E entity) { entityManager.remove(entity); }

    public E findById(K id) { return entityManager.find(entityClass, id); }
}

import org.springframework.stereotype.Repository;

@Repository
public class JpaDaoUser extends JpaDao<User, String> implements DaoUser {
}

For the JPA configuration I used the default class generated by the template:

@Configuration
public class JpaConfiguration {

    @Value("#{dataSource}")
    private javax.sql.DataSource dataSource;

    @Bean
    public Map<String, Object> jpaProperties() {
        Map<String, Object> props = new HashMap<String, Object>();
        props.put("hibernate.dialect", H2Dialect.class.getName());
        props.put("hibernate.cache.provider_class", HashtableCacheProvider.class.getName());
        return props;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
        hibernateJpaVendorAdapter.setShowSql(false);
        hibernateJpaVendorAdapter.setGenerateDdl(true);
        hibernateJpaVendorAdapter.setDatabase(Database.H2);
        return hibernateJpaVendorAdapter;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new JpaTransactionManager( localContainerEntityManagerFactoryBean().getObject() );
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean() {
        LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
        lef.setDataSource(this.dataSource);
        lef.setJpaPropertyMap(this.jpaProperties());
        lef.setJpaVendorAdapter(this.jpaVendorAdapter());
        return lef;
    }

}

At the end I modified the test class (in test folder) to test my simple project

@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class UserPersistenceTests {

    @Autowired
    public JpaDaoUser du;

    @Test
    public void test1() throws Exception {
        assertEquals("danilo", du.findById("danilo").getUsername());
    }

}

and I got the following problem

java.lang.IllegalArgumentException: Unknown entity: java.lang.String
    at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:626)
    at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:589)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
    at $Proxy26.find(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
    at $Proxy26.find(Unknown Source)
    at com.windy.spring.JpaDao.findById(JpaDao.java:23)
    at com.windy.spring.OrderPersistenceTests.testSaveAndGet(OrderPersistenceTests.java:22)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.hibernate.MappingException: Unknown entity: java.lang.String
    at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:691)
    at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:92)
    at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1090)
    at org.hibernate.impl.SessionImpl.get(SessionImpl.java:1005)
    at org.hibernate.impl.SessionImpl.get(SessionImpl.java:998)
    at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:614)
    ... 43 more

I can understand why I get this error (I checked that in my db exists the entry)??

Thanks in advance for your help

Danilo

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:52:54+00:00Added an answer on May 28, 2026 at 10:52 pm
    java.lang.IllegalArgumentException: Unknown entity: java.lang.String
       at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:626)
    

    This basically boils down to that you’re doing something like follows:

    String string = entityManager.find(String.class, id);
    

    This makes thus no sense. Let’s trackback in the code where you’re doing that. Your mistake is in the genericSuperclass.getActualTypeArguments()[1] call of the JpaDao constructor:

    public JpaDao() {
        ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
        this.entityClass = (Class<E>) genericSuperclass.getActualTypeArguments()[1];
    }
    

    Array indexes are 0-based, so you’re basically grabbing the 2nd element which is in case of JpaDao<User, String> the String. Fix it accordingly.


    As to your new problem,

    java.lang.NullPointerException
    at com.windy.spring.PersistenceTests.test1(UserPersistenceTests.java:22)

    This just means that du.findById("danilo") returned null. That’s a different problem. It at least confirms that your Hibernate and DAO part is working fine now. Most likely you just haven’t a record in the DB which matches this ID.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.