2014-01-18 84 views
0

我是新手休眠和Web服務,並在dropwizard中爲裁縫系統創建項目。 當我嘗試通過CMD運行項目中DropWizard Sites得到:org.hibernate.MappingException:無法確定類型:com.yammer.dropwizard.tailor.model.CustomerModel

INFO [2014-01-18 08:41:13,784] org.hibernate.annotations.common.Version: HCANN0 
00001: Hibernate Commons Annotations {4.0.1.Final} 
INFO [2014-01-18 08:41:13,828] org.hibernate.Version: HHH000412: Hibernate Core 
{4.1.9.Final} 
INFO [2014-01-18 08:41:13,847] org.hibernate.cfg.Environment: HHH000206: hibern 
ate.properties not found 
INFO [2014-01-18 08:41:13,850] org.hibernate.cfg.Environment: HHH000021: Byteco 
de provider name : javassist 
INFO [2014-01-18 08:41:14,076] com.yammer.dropwizard.hibernate.SessionFactoryFa 
ctory: Entity classes: [com.yammer.dropwizard.tailor.model.CoatModel, com.yammer 
.dropwizard.tailor.model.CustomerModel, com.yammer.dropwizard.tailor.model.LongS 
hirtModel, com.yammer.dropwizard.tailor.model.OrderModel, com.yammer.dropwizard. 
tailor.model.ShirtModel, com.yammer.dropwizard.tailor.model.TailorModel, com.yam 
mer.dropwizard.tailor.model.TrouserModel] 
Exception in thread "main" org.hibernate.MappingException: Could not determine t 
ype for: com.yammer.dropwizard.tailor.model.CustomerModel, at table: Order, for 
columns: [org.hibernate.mapping.Column(customer)] 
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:314) 
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:292) 
    at org.hibernate.mapping.Property.isValid(Property.java:239) 
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:4 
69) 
    at org.hibernate.mapping.RootClass.validate(RootClass.java:270) 
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1294) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.jav 
a:1742) 
    at com.yammer.dropwizard.hibernate.SessionFactoryFactory.buildSessionFac 
tory(SessionFactoryFactory.java:77) 
    at com.yammer.dropwizard.hibernate.SessionFactoryFactory.build(SessionFa 
ctoryFactory.java:35) 
    at com.yammer.dropwizard.hibernate.HibernateBundle.run(HibernateBundle.j 
ava:38) 
    at com.yammer.dropwizard.hibernate.HibernateBundle.run(HibernateBundle.j 
ava:13) 
    at com.yammer.dropwizard.config.Bootstrap.runWithBundles(Bootstrap.java: 
64) 
    at com.yammer.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.j 
ava:37) 
    at com.yammer.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.jav 
a:58) 
    at com.yammer.dropwizard.cli.Cli.run(Cli.java:53) 
    at com.yammer.dropwizard.Service.run(Service.java:61) 
    at com.yammer.dropwizard.tailor.service.TailorService.main(TailorService 
.java:25) 

類:

CustomerModel類:

@NamedQueries({ 
@NamedQuery(
    name = "com.yammer.dropwizard.tailor.model.CustomerModel.findAll", 
    query = "SELECT c FROM CustomerModel c" 
), 
@NamedQuery(
    name = "com.yammer.dropwizard.tailor.model.CustomerModel.findById", 
    query = "SELECT c FROM CustomerModel c WHERE c.ID = :ID" 
) 
}) 
@Entity 
@Table(name = "Customer") 
public class CustomerModel { 
@Id 
@GeneratedValue 
@Column(name = "c_id") 
int ID; 
@Column(name = "c_code") 
String customerCode; 
@Column(name = "c_fname") 
String firstName; 
@Column(name = "c_mname") 
String middleName; 
@Column(name = "c_lname") 
String lastName; 
@Column(name = "c_nic") 
String NIC_Number; 
@Column(name = "c_email") 
String email; 
@Column(name = "c_pnumber") 
String number; 


public int getID() { 
    return ID; 
} 

public void setID(int ID) { 
    this.ID = ID; 
} 

public String getCustomerCode() { 
    return customerCode; 
} 

public void setCustomerCode(String customerCode) { 
    this.customerCode = customerCode; 
} 

public String getFirstName() { 
    return firstName; 
} 

public void setFirstName(String firstName) { 
    this.firstName = firstName; 
} 

public String getMiddleName() { 
    return middleName; 
} 

public void setMiddleName(String middleName) { 
    this.middleName = middleName; 
} 

public String getLastName() { 
    return lastName; 
} 

public void setLastName(String lastName) { 
    this.lastName = lastName; 
} 

public String getNIC_Number() { 
    return NIC_Number; 
} 

public void setNIC_Number(String NIC_Number) { 
    this.NIC_Number = NIC_Number; 
} 

public String getEmail() { 
    return email; 
} 

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

public String getNumber() { 
    return number; 
} 

public void setNumber(String number) { 
    this.number = number; 
}} 

其中其他類應該我列出?

請幫幫我。

更多類:

數據庫配置類:

public class databaseConfiguration extends Configuration { 

@Valid 
@NotNull 
@JsonProperty 
DatabaseConfiguration dbconfigurations = new DatabaseConfiguration(); 

public DatabaseConfiguration getDatabaseConfiguration() { 
    return dbconfigurations; 
} 
} 

.YML文件

dbconfigurations: 
# the name of your JDBC driver 
driverClass: org.sqlite.JDBC 

# the username 
user: 

# the password 
password: 

url: jdbc:sqlite:TailorDB.db 

服務類:

public class TailorService extends Service<databaseConfiguration> { 

public static void main(String[] args) throws Exception { 
    new TailorService().run(args); 
} 
private final HibernateBundle<databaseConfiguration> hibernate = new HibernateBundle<databaseConfiguration>(CustomerModel.class,OrderModel.class,CoatModel.class,LongShirtModel.class,ShirtModel.class,TailorModel.class,TrouserModel.class) { 
    @Override 
    public DatabaseConfiguration getDatabaseConfiguration(databaseConfiguration configuration) { 
      return configuration.getDatabaseConfiguration(); 
    } 
}; 

@Override 
public void initialize(Bootstrap<databaseConfiguration> bootstrap) { 
    // TODO Auto-generated method stub 
    bootstrap.setName("tailor"); 
    bootstrap.addBundle(hibernate); 

} 

@Override 
public void run(databaseConfiguration configuration, Environment environment) 
     throws Exception { 
    // TODO Auto-generated method stub 

    final CustomerDAO cdao = new CustomerDAO(hibernate.getSessionFactory()); 
    final OrderDAO odao = new OrderDAO(hibernate.getSessionFactory()); 
    environment.addResource(new TailorResource(cdao,odao)); 

} 

} 
+0

分享你的hbm.xml文件 –

+0

我只是在項目中有pom.xml文件。 和我在實體classes.do中使用註釋我仍然需要hbm.xml? –

+0

哦。雅剛剛意識到。一點都不。我是一個老派的休眠用戶。 –

回答

1

第一眼後,似乎你的sessio nFactory不知道CustomerModel實體。確保將它作爲映射文件添加到sessionFactory中。

+0

休眠在Dropwizerd他們寫道 「私人最終HibernateBundle 休眠=新HibernateBundle (Person.class)」 所以我加了 HibernateBundle 休眠=新HibernateBundle (CustomerModel.class ,OrderModel.class,CoatModel.class,LongShirtModel.class,ShirtModel.class,TailorModel.class,TrouserModel.class) 進入我的項目。 –

+0

是寫嗎? 而我在實體類中使用註釋。所以我是否還需要映射文件? –

+0

還有 INFO [2014-01-18 08:41:14,076] com.yammer.dropwizard.hibernate。SessionFactoryFa ctory:實體類:[com.yammer.dropwizard.tailor.model.CoatModel,com.yammer .dropwizard.tailor.model.CustomerModel,com.yammer.dropwizard.tailor.model.LongS hirtModel,com.yammer .dropwizard.tailor.model.OrderModel,com.yammer.dropwizard。 tailor.model.ShirtModel,com.yammer.dropwizard.tailor.model.TailorModel,com.yam mer.dropwizard.tailor.model.TrouserModel] –

1

從其他建議的答案:「請確保它作爲映射文件添加到sessionFactory。」 這可能正是Dropwizard試圖避免的。然而,他們在他們的教程頁這裏做了一個非常糟糕的工作http://dropwizard.codahale.com/manual/hibernate/

基本上,按照該頁面,這個錯誤是保證你會得到什麼。因爲它沒有涵蓋非常重要的部分。有人太粗心大意或懶得複製粘貼他們在教程中使用的「Person」類。下面是從https://github.com/dropwizard/dropwizard/blob/master/dropwizard-example/src/main/java/com/example/helloworld/core/Person.java

package com.example.helloworld.core; 

import javax.persistence.*; 

@Entity 
@Table(name = "people") 
@NamedQueries({ 
    @NamedQuery(
     name = "com.example.helloworld.core.Person.findAll", 
     query = "SELECT p FROM Person p" 
    ), 
    @NamedQuery(
     name = "com.example.helloworld.core.Person.findById", 
     query = "SELECT p FROM Person p WHERE p.id = :id" 
    ) 
}) 
public class Person { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private long id; 

    @Column(name = "fullName", nullable = false) 
    private String fullName; 

    @Column(name = "jobTitle", nullable = false) 
    private String jobTitle; 

    public long getId() { 
     return id; 
    } 

    public void setId(long id) { 
     this.id = id; 
    } 

    public String getFullName() { 
     return fullName; 
    } 

    public void setFullName(String fullName) { 
     this.fullName = fullName; 
    } 

    public String getJobTitle() { 
     return jobTitle; 
    } 

    public void setJobTitle(String jobTitle) { 
     this.jobTitle = jobTitle; 
    } 
} 

現在你可以看到在頂部,有一個註釋說明了這個類是實體和表名。我花了2個小時試圖理解這個問題。他們在想什麼離開了教程!

相關問題