0

I recently started my first Java project that uses Hibernate (and JPA Annotations) for persistence.

I've got the following classes:

User class

*some imports*
@Entity
public class Owner {
        private int id;
        private String name;
        @OneToOne(fetch=FetchType.LAZY)
        @JoinColumn(name="id")
        private Pet pet;

        @Id
        @GeneratedValue
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }

        public void setPet(Pet pet) {
            this.pet = pet;
        }
        public Pet getPet() {
            return pet;
        }
    }

Pet Class

*some imports*
@Entity 
public class Pet {
        private String name;
        @Id
        @GeneratedValue
        private int id;

        public void setId(int id) {
            this.id = id;
        }
        public int getId() {
            return id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }

Whenever I try to save an object, Hibernate gives me the following error output.

Exception in thread "main" org.hibernate.MappingException: Could not determine type for: Pet, at table: Owner, for columns: [org.hibernate.mapping.Column(pet)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:306)
at org.hibernate.mapping.Column.getSqlTypeCode(Column.java:164)

What am I missing? I can't figure out how to do this PF-FK mapping.

Do I need to tell Hibernate which table Pet maps into or is it smart enough to do that? Could someone please suggest how I can change my declaration of the Pet ivar so that Hibernate and I can be happy? :)

Thanks!

Julian
  • 1,573
  • 2
  • 22
  • 36

2 Answers2

4

I guess it's caused by the fact that you mixed annotations on fields and properties. You need to place annotations in a consistent way, unless you explicitly indicate how they are placed with @Access/@AccessType.

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • I placed all @Annotations consistently on the ivars. The error output has now changed to: Exception in thread "main" java.lang.NoSuchMethodError: javax.persistence.OneToOne.orphanRemoval()Z – Julian Jun 08 '11 at 19:54
  • http://stackoverflow.com/questions/2511997/i-am-getting-an-error-with-a-onetomany-association-when-using-annotations-with-gi The accepted answer solved my problem :) Thanks! – Julian Jun 08 '11 at 20:08
1

When it says it doesn't know the entity, that generally means it has either not found it (if you are using scanning) or you forgot to list it. If you are using JPA, generally you will list the entities in persistence.xml (which is in the META-INF folder).

Rob
  • 11,446
  • 7
  • 39
  • 57
  • Should add that sometimes you see this message when the related item does not have an @Entity annotation but that's clearly not the case here. – Rob Jun 08 '11 at 19:47
  • I am not using a mapping XML since I thought that annotation tags were all about not having to use one. I feel like I'm missing something really important here ... And could you please explain how I can use scanning? Thanks :) – Julian Jun 08 '11 at 19:51
  • You can tell Hibernate and JPA to scan for entities in which case you do not have to list them in persistence.xml. The problem with this approach is that the scanner is only going to scan the current project. If you have for instance your domain objects in a jar project and then include that jar in a war project, your war project scanner is NOT going to find the entities in the jar. The Spring guys made a scanner you can use. (If you have Spring in your stack.) A ton of people bitched about this in JPA 1. – Rob Jun 08 '11 at 23:59
  • 1
    You do not need a mapping XML. If you have scanning turned on (you have to configure that in your persistence unit definition wherever that is, persistence.xml or you could be configuring it in Spring or Seam) you could do it there. By default, it will scan from the parent folder of where the META-INF is located. Maybe your second entity is in the test tree by mistake? – Rob Jun 09 '11 at 00:05